Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect access modifier type on a property using Reflection

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?

like image 210
Michael Edwards Avatar asked Mar 11 '10 15:03

Michael Edwards


People also ask

What is GetProperty C#?

GetProperty(String) Searches for the public property with the specified name. GetProperty(String, BindingFlags) Searches for the specified property, using the specified binding constraints.

Can we have different access modifiers on Get Set methods of a property?

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.

What is reflection C#?

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.


3 Answers

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.

like image 83
Anton Gogolev Avatar answered Oct 30 '22 16:10

Anton Gogolev


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..

like image 26
Richard Friend Avatar answered Oct 30 '22 17:10

Richard Friend


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
}
like image 28
Johan Larsson Avatar answered Oct 30 '22 17:10

Johan Larsson