2010-03-11 21 views
15

Tôi đã viết một số mã để xem xét các thuộc tính sử dụng sự phản chiếu. Tôi đã lấy ra một danh sách các thuộc tính từ lớp bằng cách sử dụng sự phản chiếu.Bộ chỉnh sửa phản xạ và truy cập trên thuộc tính

Tuy nhiên, tôi cần phải tìm hiểu xem thuộc tính có công khai hay được bảo vệ hay không. ví dụ:

public string Name{get;set;} 
protected int Age{get;set;} 

Lớp PropertyInfo dường như không tiết lộ thông tin này về tài sản. Có một cách khác để làm điều này?

Trả lời

22

Vì các thuộc tính chỉ là đường cú pháp trên một phương thức get/set, không có thứ như "khả năng truy cập" của thuộc tính phản chiếu khôn ngoan. Thay vào đó, bạn sẽ phải tìm ra các mức truy cập của các phương thức getset riêng biệt. Để kết thúc, hãy truy xuất các đối tượng thích hợp MethodInfo với các phương thức GetGetMethodGetSetMethod và từ đó có IsPrivate, IsPublic và các phương pháp và thuộc tính khác.

+0

bạn đánh bại tôi với nó ... –

9

Bạn cần phải nhìn vào các MethodInfo của mỗi trường nhận & phương thức thiết lập ví dụ:

PropertyInfo pi = ...; 
bool isPublic = pi.GetGetMethod(true).IsPublic; 
bool isProtected= pi.GetGetMethod(true).IsFamily; 

Nó có vẻ là tài sản IsFamily cho biết nếu một phương pháp được bảo vệ ..

0
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(); 
    } 
0

Đã viết phương thức mở rộng cho điều này:

public static class ReflectionExt 
{ 
    public static readonly List<AccessModifier> AccessModifiers = new List<AccessModifier> 
    { 
     AccessModifier.Private, 
     AccessModifier.Protected, 
     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.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 
} 
Các vấn đề liên quan