Detect access modifier type on a property using Reflection

天涯浪子 提交于 2020-01-12 06:42:15

问题


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?


回答1:


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.




回答2:


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




回答3:


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
}



回答4:


    static void Main()
    {
        sample obj = new sample();
        Type t = obj.GetType();
        MethodInfo[] m = t.GetMethods();
        Console.WriteLine("Method Information:-\n\n");
        foreach (MethodInfo m1 in m)
            Console.WriteLine("Mthod name:" + m1.Name + "  \nMethod return type:" + m1.ReturnType + "\nIs staic:" + m1.IsStatic + "\nIs Public:" + m1.IsPublic + "\nIs Private:" + m1.IsPrivate);
        FieldInfo[] f = t.GetFields();
        Console.WriteLine("\n\nField Information:-\n\n");
        foreach(FieldInfo f1 in f)
            Console.WriteLine("Field name:" + f1.Name + "  \nField type:" + f1.FieldType + "\nIs staic:" + f1.IsStatic);
        Console.Read();
    }


来源:https://stackoverflow.com/questions/2426134/detect-access-modifier-type-on-a-property-using-reflection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!