Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

> NeoDataType.net <

Try my Free .Net Reporting Tool!

  • *Experts*
Posted

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:

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.

  • *Experts*
Posted

Fields are implemented as two properties (which are wrappers for methods as Volte mentioned). In case that helps :)

 

-Ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
  • *Experts*
Posted

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.

Posted

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

> NeoDataType.net <

Try my Free .Net Reporting Tool!

Posted
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 :-\ )

> NeoDataType.net <

Try my Free .Net Reporting Tool!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...