利用反射查看类成员

反射查看类的成员信息

利用C#的反射机制,可以查看封装的类型的基本信息及元数据。本文中的示例则是利用反射机制来查看类的成员信息,包括字段,方法,构造函数。

下面先列举示例中所用到的类

Type :  System.Reflection 功能的根,也是访问元数据的主要方式。使用 Type 的成员获取关于类型声明的信息,如构造函数、方法、字段、属性 (Property) 和类的事件,以及在其中部署该类的模块和程序集。(摘自MSDN)

PropertyInfo : 发现属性 (Property) 的属性 (Attribute) 并提供对属性 (Property) 元数据的访问。

ParameterInfo:发现参数属性 (Attribute) 并提供对参数元数据的访问。

ConstructorInfo :用于发现构造函数的属性 (Attribute) 及调用构造函数。通过对 ConstructorInfo 调用 Invoke 来创建对象,其中ConstructorInfo 是由 Type 对象的 GetConstructors 或 GetConstructor 方法返回的。

下面列出示例

被反射的类 Human类

Human
 1     public class Human
 2     {
 3         private string name;
 4 
 5         private int age;
 6 
 7         private bool sex;
 8 
 9         public string Name
10         {
11             get { return name; }
12             set { name = value; }
13         }
14 
15         public int Age
16         {
17             get { return age; }
18             set {
19                 if (value > 0) age = value;
20                 else age = 0;
21             }
22         }
23 
24         public bool Sex
25         {
26             get { return sex; }
27             set { sex = value; }
28         }
29 
30         public Human()
31         { }
32 
33         public Human(string iName, int iAge, bool iSex)
34         {
35             this.Name = iName;
36             this.Age = iAge;
37             this.Sex = iSex;
38         }
39 
40         public void Introduce()
41         {
42             Console.WriteLine("Hello my name is {0} ,I'm {1} .", Name, Age);
43         }
44 
45         public bool GuessMySex(Sex iSex)
46         {
47             return Convert.ToBoolean( iSex) == Sex;
48         }
49     }

辅助的枚举

View Code
1     public enum  Sex:int
2     { 
3         Man=1,
4 
5         Woman=0
6     }

Main方法

        static void Main(string[] args)
        {
            Human test = new Human();
            Type type = ((object)test).GetType();
            PropertyInfo[] proInfos = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            Console.WriteLine("-----------------字段---------------");

            foreach (PropertyInfo pi in proInfos)
                Console.WriteLine("{0} {1} {2} : {3} ;", pi.Attributes, pi.PropertyType, pi.Name, pi.GetValue(test, null));

            MethodInfo[] methInfo = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);

            ConstructorInfo[] consInfo= type.GetConstructors();

            Console.WriteLine("----------构造函数------------");

            foreach (ConstructorInfo ci in consInfo)
            {
                Console.Write("{0} {1} (", ci.Attributes, type.Name);
                ParameterInfo[] pinfos = ci.GetParameters();
                string paramString = string.Empty;
                foreach (ParameterInfo pi in pinfos)
                {
                    paramString += string.Format("{0} {1} {2},", pi.Attributes, pi.ParameterType, pi.Name);
                }
                Console.WriteLine(paramString.Trim(',') + ");");
            }

            Console.WriteLine("---------------方法----------");

            foreach (MethodInfo mi in methInfo)
            {
                Console.Write("{0} {1} {2} (", mi.Attributes, mi.ReturnType, mi.Name);
                ParameterInfo[] pinfos = mi.GetParameters();
                string paramString = string.Empty;
                foreach (ParameterInfo pi in pinfos)
                {
                    paramString += string.Format("{0} {1} {2},", pi.Attributes, pi.ParameterType, pi.Name);
                }
                Console.WriteLine(paramString.Trim(',') + ");");

            }
            Console.ReadLine();
        }

 运行结果:

-----------------字段---------------
None System.String Name :  ;
None System.Int32 Age : 0 ;
None System.Boolean Sex : False ;
----------构造函数------------
PrivateScope, Public, HideBySig, SpecialName, RTSpecialName Human ();
PrivateScope, Public, HideBySig, SpecialName, RTSpecialName Human (None System.String iName,None System.Int32 iAge,None System.Boolean iSex);
---------------方法----------
PrivateScope, Public, HideBySig, SpecialName System.String get_Name ();
PrivateScope, Public, HideBySig, SpecialName System.Void set_Name (None System.String value);
PrivateScope, Public, HideBySig, SpecialName System.Int32 get_Age ();
PrivateScope, Public, HideBySig, SpecialName System.Void set_Age (None System.Int32 value);
PrivateScope, Public, HideBySig, SpecialName System.Boolean get_Sex ();
PrivateScope, Public, HideBySig, SpecialName System.Void set_Sex (None System.Boolean value);
PrivateScope, Public, HideBySig System.Void Introduce ();
PrivateScope, Public, HideBySig System.Boolean GuessMySex (None Client.Sex iSex)
;
PrivateScope, Public, Virtual, HideBySig, VtableLayoutMask System.String ToString ();
PrivateScope, Public, Virtual, HideBySig, VtableLayoutMask System.Boolean Equals (None System.Object obj);
PrivateScope, Public, Virtual, HideBySig, VtableLayoutMask System.Int32 GetHashCode ();
PrivateScope, Public, HideBySig System.Type GetType ();

 

posted @ 2012-09-02 13:06  猴健居士  阅读(623)  评论(0编辑  收藏  举报