Reflection question

neodatatype

Well-known member
Joined
Aug 18, 2003
Messages
68
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 its read/write only) called get_MyProperty and set_MyProperty. Youll 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:
Code:
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.
 
Fields are implemented as two properties (which are wrappers for methods as Volte mentioned). In case that helps :)

-Ner
 
What do you mean by that Nerseus? Fields arent 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 fields DeclaringType is Enum (with fi.DeclaringType.IsEnum). You can use fi.IsLiteral AndAlso fi.IsStatic to check to see if its 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
 
Just retrieve all of the constant fields, instead of all the members.
 
Originally posted by VolteFace
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 Im not boring you :-\ )
 
Back
Top