I have written some code to look at properties using reflection. I have retrieved a list of properties from the class using reflection.
However I need to find out if the property is public or protected. eg:
public string Name{get;set;}
protected int Age{get;set;}
The PropertyInfo class does not seem to expose this information about the property. Is there another way to do this?
GetProperty(String) Searches for the public property with the specified name. GetProperty(String, BindingFlags) Searches for the specified property, using the specified binding constraints.
IS it possible to have different access modifiers on the get/set methods of a property? No. The access modifier on a property applies to both its get and set accessors.
Reflection provides objects (of type Type) that describe assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.
Since properties are just syntactic sugar over a pair of get
/set
methods, there's no such thing as "accessibility" of a property reflection-wise. Rather, you'll have to find out accessibility levels of get
and set
methods separately. To that end, retrieve appropriate MethodInfo
objects with GetGetMethod
and GetSetMethod
methods, and from there are various IsPrivate
, IsPublic
and other methods and properties.
You need to look at the methodInfo of each get & set method eg :
PropertyInfo pi = ...;
bool isPublic = pi.GetGetMethod(true).IsPublic;
bool isProtected= pi.GetGetMethod(true).IsFamily;
It seems to be the IsFamily property that indicates if a method is protected..
Wrote an extension method for this:
public static class ReflectionExt
{
public static readonly List<AccessModifier> AccessModifiers = new List<AccessModifier>
{
AccessModifier.Private,
AccessModifier.Protected,
AccessModifier.ProtectedInternal,
AccessModifier.Internal,
AccessModifier.Public
};
public static AccessModifier Accessmodifier(this PropertyInfo propertyInfo)
{
if (propertyInfo.SetMethod == null)
return propertyInfo.GetMethod.Accessmodifier();
if (propertyInfo.GetMethod == null)
return propertyInfo.SetMethod.Accessmodifier();
var max = Math.Max(AccessModifiers.IndexOf(propertyInfo.GetMethod.Accessmodifier()),
AccessModifiers.IndexOf(propertyInfo.SetMethod.Accessmodifier()));
return AccessModifiers[max];
}
public static AccessModifier Accessmodifier(this MethodInfo methodInfo)
{
if (methodInfo.IsPrivate)
return AccessModifier.Private;
if (methodInfo.IsFamily)
return AccessModifier.Protected;
if (methodInfo.IsFamilyOrAssembly)
return AccessModifier.ProtectedInternal;
if (methodInfo.IsAssembly)
return AccessModifier.Internal;
if (methodInfo.IsPublic)
return AccessModifier.Public;
throw new ArgumentException("Did not find access modifier", "methodInfo");
}
}
public enum AccessModifier
{
Private,
Protected,
Internal,
Public
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With