HighTechTalks DotNet Forums  

GetProperty BindingFlags question concerning Friend properties

Dotnet Framework (CLR) microsoft.public.dotnet.framework.clr


Discuss GetProperty BindingFlags question concerning Friend properties in the Dotnet Framework (CLR) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Rob Nicholson
 
Posts: n/a

Default GetProperty BindingFlags question concerning Friend properties - 05-13-2006 , 08:53 AM






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.



Reply With Quote
  #2  
Old   
Barry Kelly
 
Posts: n/a

Default Re: GetProperty BindingFlags question concerning Friend properties - 05-13-2006 , 09:57 AM






"Rob Nicholson" <rob.nicholson (AT) nospam_unforgettable (DOT) com> wrote:

Quote:
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!
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


Reply With Quote
  #3  
Old   
Rob Nicholson
 
Posts: n/a

Default Re: GetProperty BindingFlags question concerning Friend properties - 05-13-2006 , 12:47 PM



Quote:
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.




Reply With Quote
  #4  
Old   
Ben Voigt
 
Posts: n/a

Default Re: GetProperty BindingFlags question concerning Friend properties - 05-13-2006 , 12:49 PM



"Barry Kelly" <barry.j.kelly (AT) gmail (DOT) com> wrote

Quote:
"Rob Nicholson" <rob.nicholson (AT) nospam_unforgettable (DOT) com> wrote:

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...

Quote:
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!

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



Reply With Quote
  #5  
Old   
Ben Voigt
 
Posts: n/a

Default Re: GetProperty BindingFlags question concerning Friend properties - 05-13-2006 , 06:24 PM



"Rob Nicholson" <rob.nicholson (AT) nospam_unforgettable (DOT) com> wrote

Quote:
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

Quote:
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.




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.