using System;
using System.Reflection;//反射
public class FieldInfoClass
{
public int myField1 = 0;
protected string myField2 = null;//保护成员,class and subclass can access
public static void Main() //static main method
{
FieldInfo[] myFieldInfo;//数组类
Type myType = typeof(FieldInfoClass);//自生类的类类型提取
// Get the type and fields of FieldInfoClass.
//基于以上自生类类型提取类的属性,返回fieldinfo[]
//由type.getfields方法返回fieldinfo类型,由此可以提取类的相关属性定义信息
//方法参数表示SEARCH的范围
myFieldInfo = myType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance
| BindingFlags.Public);
Console.WriteLine("\nThe fields of " +
"FieldInfoClass are \n");
// Display the field information of FieldInfoClass.
//fieldinfo[].length提取类类型中属性的个数
for (int i = 0; i < myFieldInfo.Length; i++)
{
Console.WriteLine("\nName : {0}", myFieldInfo[i].Name);
Console.WriteLine("Declaring Type : {0}", myFieldInfo[i].DeclaringType);
Console.WriteLine("IsPublic : {0}", myFieldInfo[i].IsPublic);
Console.WriteLine("MemberType : {0}", myFieldInfo[i].MemberType);
Console.WriteLine("FieldType : {0}", myFieldInfo[i].FieldType);
Console.WriteLine("IsFamily : {0}", myFieldInfo[i].IsFamily);
Console.ReadKey();
}
}
}