neodatatype Posted August 19, 2003 Posted August 19, 2003 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 Quote > NeoDataType.net < Try my Free .Net Reporting Tool!
*Experts* Volte Posted August 19, 2003 *Experts* Posted August 19, 2003 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 FunctionI changed it a bit for your purposes (mine returned an Enum type specific to my program), but the functionality is there. Quote
neodatatype Posted August 19, 2003 Author Posted August 19, 2003 Here is a method I used in one of my programs to get the visibility: Woah! :) This seems good, I'll try it and I hope it'll work also with fields and methods :) Thanx! Quote > NeoDataType.net < Try my Free .Net Reporting Tool!
*Experts* Nerseus Posted August 19, 2003 *Experts* Posted August 19, 2003 Fields are implemented as two properties (which are wrappers for methods as Volte mentioned). In case that helps :) -Ner Quote "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* Volte Posted August 19, 2003 *Experts* Posted August 19, 2003 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. Quote
neodatatype Posted August 21, 2003 Author Posted August 21, 2003 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 Quote > NeoDataType.net < Try my Free .Net Reporting Tool!
*Experts* Volte Posted August 21, 2003 *Experts* Posted August 21, 2003 Just retrieve all of the constant fields, instead of all the members. Quote
neodatatype Posted August 21, 2003 Author Posted August 21, 2003 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 :-\ ) Quote > NeoDataType.net < Try my Free .Net Reporting Tool!
neodatatype Posted August 21, 2003 Author Posted August 21, 2003 But how can I know that the object is an enumerator and not a class? Ok, it seems to work fine! :) obj.IsEnum and member.IsLiteral Right? Thanks again. Quote > NeoDataType.net < Try my Free .Net Reporting Tool!
neodatatype Posted August 21, 2003 Author Posted August 21, 2003 Maybe you can tell me how to filter the get_ and set_ members :mad: Bye Quote > NeoDataType.net < Try my Free .Net Reporting Tool!
*Experts* Volte Posted August 21, 2003 *Experts* Posted August 21, 2003 Try the .IsSpecialName property of the MethodInfo. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.