在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
可能大家都知道在C#中如何创建一个类,但对于类的结构可能大家不一定了解的很清楚,对于我来说,我之前也搞的不是很明白,今天,当我没事研究反射的时候突然发现了着一点。 public class People //类名 { private static string name; //字段 private string sex;//字段 public string Sex //属性 { get { return sex; } set { sex = value; } } public static string Name //属性 { get { return People.name; } set { People.name = value; } } private People() //构造函数 { } public static string GetName() // 函数 { if (string.IsNullOrEmpty(name)) { name = "my name"; } return name; } }
Type t = typeof(People); Console.WriteLine("----------------Method------------------"); MethodInfo[] methods = t.GetMethods(); foreach (MethodInfo method in methods) { Console.WriteLine("Method:" + method); //Console.WriteLine(method); //Console.WriteLine("返回值:" + method.ReturnParameter); } Console.WriteLine("---------------Field-------------------"); FieldInfo[] fields = t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); foreach (FieldInfo field in fields) { Console.WriteLine("Field:" + field); } Console.WriteLine("--------------Member--------------------"); MemberInfo[] members = t.GetMembers(); foreach (MemberInfo member in members) { Console.WriteLine("Member:" + member); } Console.WriteLine("--------------Property--------------------"); PropertyInfo[] properties = t.GetProperties(); foreach (PropertyInfo property in properties) { Console.WriteLine("Property:" + property); } Console.WriteLine("--------------Constructor--------------------"); ConstructorInfo[] constructors = t.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance); foreach (ConstructorInfo constructor in constructors) { Console.WriteLine("Constructor:" + constructor); }
----------------Method------------------
Method:System.String get_Sex()
Method:Void set_Sex(System.String)
Method:System.String get_Name()
Method:Void set_Name(System.String)
Method:System.String GetName()
Method:System.String ToString()
Method:Boolean Equals(System.Object)
Method:Int32 GetHashCode()
Method:System.Type GetType()
---------------Field-------------------
Field:System.String sex
Field:System.String name
--------------Member--------------------
Member:System.String get_Sex()
Member:Void set_Sex(System.String)
Member:System.String get_Name()
Member:Void set_Name(System.String)
Member:System.String GetName()
Member:System.String ToString()
Member:Boolean Equals(System.Object)
Member:Int32 GetHashCode()
Member:System.Type GetType()
Member:System.String Sex
Member:System.String Name
--------------Property--------------------
Property:System.String Sex
Property:System.String Name
--------------Constructor--------------------
Constructor:Void.ctor()
|
请发表评论