Difference PropertyInfo, FieldInfo, MemberInfo the [Finishing] C # reflection (the Reflection) Detailed

 

 

 

public class TestClass
{
    private int a = 1;//私有一律获取不到
    public int b
    {
        get { return 2; }
        set { value = 2; }
    }
    public int c = 3;
}

public static void TestMethod()
{
    TestClass test = new TestClass();
    PropertyInfo[] pro = test.GetType().GetProperties();
    FieldInfo[] fil = Test.GetType () GetFields ();. 
    The MemberInfo [] Men = test.GetType () GetMembers ();. 

    The foreach ( var Item in Pro) // only acquired property b (output = 2 b) 
    { 
        Console. the WriteLine ( " PropertyInfo: " + item.name + " = " + item.GetValue (Test, null )); 
    } 
    the foreach (the FieldInfo Item in FIL) // only acquired field c (output = 2 c) 
    { 
        Console. the WriteLine ( " the FieldInfo: " + item.name + " =" + Item.GetValue (Test)); 
    } 
    the foreach (the MemberInfo Item in FIL) // only acquired field c (output c) 
    { 
        Console.WriteLine ( " the MemberInfo: " + item.name); 
    } 
}

 

 

 

 

reference

[Finishing] C # reflection (the Reflection) Detailed

Guess you like

Origin www.cnblogs.com/code1992/p/11348064.html