> 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