Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / May 2006

Tip: Looking for answers? Try searching our database.

GetProperty BindingFlags question concerning Friend properties

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rob Nicholson - 13 May 2006 13:53 GMT
I've using Reflection to get a value from a property in a class and I needed
to modify it to allow Friend properties to be included:

PropertyInfo = Me.GetType.GetProperty(Name, BindingFlags.Public Or
BindingFlags.NonPublic Or BindingFlags.Instance)

Now I understand what the Public & NonPublic flags are doing but without the
Instance flag as well, it doesn't work. Can anyone please explain why adding
Instance makes it work? MSDN is a bit vague about what the instance flag
does (to me anyway):

"Specifies that instance members are to be included in the search"

I think I know what an instance is but maybe not!

Thanks, Rob.
Barry Kelly - 13 May 2006 14:57 GMT
> I've using Reflection to get a value from a property in a class and I needed
> to modify it to allow Friend properties to be included:
[quoted text clipped - 10 lines]
>
> I think I know what an instance is but maybe not!

Instance members are members that aren't static. Static members are
declared (in C#) with the 'static' keyword, I think it's 'Shared' in VB.

Instance members are members which need to be applied to an instance.
For example, the Length property of System.String is an instance
property. You can't simply call 'String.Length', because it isn't a
static property, it's an instance property.

On the other hand, the 'String.Empty' field is a static field, and can't
be applied to an instance. You can't use:

"foo".Empty

as an expression, since the Empty field is static and can't be applied
to an instance.

-- Barry
Rob Nicholson - 13 May 2006 17:46 GMT
> Instance members are members that aren't static. Static members are
> declared (in C#) with the 'static' keyword, I think it's 'Shared' in VB.

Okay, I understand that now but why does applying the Instance flag to
GetProperty effect allow you to use:

Friend ReadOnly Property SomeProperty() As Boolean

Without the Instance flag, the property has to be public:

Public ReadOnly Property SomeProperty() As Boolean

Neither of these two are static.

Cheers, Rob.
Ben Voigt - 13 May 2006 23:24 GMT
>> Instance members are members that aren't static. Static members are
>> declared (in C#) with the 'static' keyword, I think it's 'Shared' in VB.
>
> Okay, I understand that now but why does applying the Instance flag to
> GetProperty effect allow you to use:

Are you comparing
GetProperty("SomeProperty", BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance)
with
GetProperty("SomeProperty", BindingFlags.Public | BindingFlags.NonPublic)
or
GetProperty("SomeProperty", /* no arguments */)

I think the second will not return any properties, even public ones... the
default if no argument is specified is (BindingFlags.Public |
BindingFlags.Static | BindingFlags.Instance), I opened mscorlib in .NET
Reflector to find that out:

Public Function GetProperty(ByVal name As String) As PropertyInfo
     If (name Is Nothing) Then
           Throw New ArgumentNullException("name")
     End If
     Return Me.GetPropertyImpl(name, (BindingFlags.Public Or
(BindingFlags.Static Or BindingFlags.Instance)), Nothing, Nothing, Nothing,
Nothing)
End Function

> Friend ReadOnly Property SomeProperty() As Boolean
>
[quoted text clipped - 5 lines]
>
> Cheers, Rob.
Ben Voigt - 13 May 2006 17:49 GMT
>> I've using Reflection to get a value from a property in a class and I
>> needed
>> to modify it to allow Friend properties to be included:
>>
>> PropertyInfo = Me.GetType.GetProperty(Name, BindingFlags.Public Or
>> BindingFlags.NonPublic Or BindingFlags.Instance)

In C# there is no instance keyword, because a property is an instance
property exactly when it isn't static.

In reflection they force you to specify either Instance or Static instead of
defaulting to instance when static isn't specified, because you might want a
list including both (think an object browser like .NET reflector, which
lists everything).  Although I do think if neither static nor instance is
specfied, they could default to instance...

>> Now I understand what the Public & NonPublic flags are doing but without
>> the
[quoted text clipped - 24 lines]
>
> -- Barry

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.