Reflection question

neodatatype

Regular
Joined
Aug 18, 2003
Messages
65
Location
Italy
Hi all!

A little question on System.Reflection.

I know that with the MethodInfo I can get if a method is public or private.

But for properties?
PropertyInfo does not contain a IsPublic property!

How can I get this information for a property?

Thanx
 
Properties are sort of misleading; there really are no such things as "properties" interally. Properties are just made of up two accessor methods (or one, if it's read/write only) called get_MyProperty and set_MyProperty. You'll need to get both of these and get *their* visibility.

Here is a method I used in one of my programs to get the visibility:
Visual Basic:
Public Shared Function GetPropertyVisiblity(ByVal pi As PropertyInfo) As Integer
        Dim mSetMethod As MethodInfo
        Dim mGetMethod As MethodInfo
        Dim mCheck As MethodInfo

        'If the property is not write-only, get its "Get" method
        If pi.CanRead Then mGetMethod = pi.GetGetMethod(True)

        'If the property is not read-only, get its "Set" method
        If pi.CanWrite Then mSetMethod = pi.GetSetMethod(True)

        'If there is no "set" method, use the "get" method, and vice versa
        If mSetMethod Is Nothing Then
            mCheck = mGetMethod
        ElseIf mGetMethod Is Nothing Then
            mCheck = mSetMethod
        Else
            mCheck = mSetMethod
        End If

        'Get the attributes of the method
        Dim mAttr As MethodAttributes = mCheck.Attributes And MethodAttributes.MemberAccessMask

        'Check its visibility 
        If (mAttr And MethodAttributes.Public) = MethodAttributes.Public Then
            Return 1 'public
        ElseIf (mAttr And MethodAttributes.Family) = MethodAttributes.Family Then
            Return 2 'protected
        ElseIf (mAttr And MethodAttributes.Assembly) = MethodAttributes.Assembly Then
            Return 3 'internal
        ElseIf (mAttr And MethodAttributes.Private) = MethodAttributes.Private Then
            Return 4 'private
        End If
End Function
I changed it a bit for your purposes (mine returned an Enum type specific to my program), but the functionality is there.
 
What do you mean by that Nerseus? Fields aren't wrappers for anything, they are just class-level variables, and as such, they have their own attributes (Public, Assembly, etc). You can also check to see if the field is an enum item by seeing if the field's DeclaringType is Enum (with fi.DeclaringType.IsEnum). You can use fi.IsLiteral AndAlso fi.IsStatic to check to see if it's a constant.

I will be releasing an Object Browser in the near future in the Code Library which acts like the .NET internal Object Browser and it will show how to do all of this.
 
Here is a method I used in one of my programs to get the visibility:

Another question.

When I enumerate members of an "enum" I get also all the derived ones, such as ToString() ecc...

There is a way to get only the "values"?

Thanx again
 
VolteFace said:
Just retrieve all of the constant fields, instead of all the members.

mmmh, good idea.
But how can I know that the object is an enumerator and not a class?

(hehe I hope I'm not boring you :-\ )
 
Back
Top