反射先了解
一:system.Type
获取基本信息:
Type.Name //类名
Type.FullName //完整路径
Type.Namespace //空间名
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class student { public int Id { set; get; } public string Name { set; get; } public int Age { set; get; } } static void Main(string[] args) { student s = new student() { Id = 1, Name = "zzj", Age = 12 }; Type t = typeof(student); //查看类信息 Console.WriteLine("Name={0},FullName={1},Namespace={2}", t.Name, t.FullName, t.Namespace); }
效果图:
Name=student
FullName=win32.反射+student
Namespace=win32
二。反射下的几个重要方法
System.Reflection.
PropertyInfo:封装类型属性信息
ConstructorInfo:类型构造函数
MethodInfo:类型的方面名称
EventInfo:类型事件信息
ParameterInfo:方法、构造函数信息
MemberInfo:成员类型 Type.GetMembers()/GetMember()/FindMember()
1.成员信息 与 memberInfo
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
StringBuilder sb = new StringBuilder(); MemberInfo[] me = t.GetMembers();//get page member sb.Append("查看类型" + t.Name + t.Name + "的成员信息"); foreach (MemberInfo m in me) { sb.Append("成员"+m.ToString().PadRight(50)+"类型:"+m.MemberType+" "); } Console.WriteLine(sb);
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
查看类型studentstudent的成员信息成员Void set_Id(Int32) 类型:Method 成员Int32 get_Id() 类型:Method 成员Void set_Name(System.String) 类型:Method 成员System.String get_Name() 类型:Method 成员Void set_Age(Int32) 类型:Method 成员Int32 get_Age() 类型:Method 成员System.String ToString() 类型:Method 成员Boolean Equals(System.Object) 类型:Method 成员Int32 GetHashCode() 类型:Method 成员System.Type GetType() 类型:Method 成员Void .ctor() 类型:Constructor 成员Int32 Id 类型:Property 成员System.String Name 类型:Property 成员Int32 Age 类型:Property 成员win32.反射+student+major 类型:NestedType 请按任意键继续. . .
2.字段信息与FieldInfo类型
如果希望获取一个类型的所有字段,可以使用GetFields()方法,再次添加一个方法
FieldExplore():(但是感觉只能获取属性,还有静态)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
static void FieldExplor(Type t) { StringBuilder sb = new StringBuilder(); FieldInfo[] fields = t.GetFields(); sb.Append("查看类型" + t.Name + "的字段信息: "); sb.Append(string.Empty.PadLeft(50, '-') + " "); foreach (FieldInfo fi in fields) { sb.Append("名称:" + fi.Name + " "); sb.Append("类型:" + fi.FieldType + " "); sb.Append("特性:" + fi.Attributes + " "); } Console.WriteLine(sb.ToString()); }
查看类型student的字段信息:
--------------------------------------------------
名称:love
类型:System.String
特性:Public
名称:Lv
类型:System.Int32
特性:Public, Static
3.属性信息与PropertyInfo类型
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
static void PropertydExplor(Type t) { StringBuilder sb = new StringBuilder(); PropertyInfo[] fields = t.GetProperties(); sb.Append("查看类型" + t.Name + "的属性信息: "); sb.Append(string.Empty.PadLeft(50, '-') + " "); foreach (PropertyInfo fi in fields) { sb.Append("名称:" + fi.Name + " "); sb.Append("类型:" + fi.PropertyType + " "); sb.Append("可读:" + fi.CanRead + " "); sb.Append("可写:" + fi.CanWrite + " "); sb.Append("特性:" + fi.Attributes + " "); } Console.WriteLine(sb.ToString()); }
查看类型student的属性信息:
--------------------------------------------------
名称:Id
类型:System.Int32
可读:True
可写:True
特性:None
名称:Name
类型:System.String
可读:True
可写:True
特性:None
名称:Age
类型:System.Int32
可读:True
可写:True
特性:None
4.方法信息与MethodInfo类型
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
static void MethodExplore(Type t) { StringBuilder sb = new StringBuilder(); MethodInfo[] me = t.GetMethods();//get page member sb.Append("查看类型" + t.Name + t.Name + "的成员信息"); foreach (MethodInfo m in me) { sb.Append("名称:" + m.Name + " "); sb.Append("签名:" + m.ToString() + " "); sb.Append("属性:" + m.Attributes + " "); sb.Append("返回值类型:" + m.ReturnType + " "); } Console.WriteLine(sb); }
查看类型studentstudent的成员信息名称:set_Id
签名:Void set_Id(Int32)
属性:PrivateScope, Public, HideBySig, SpecialName
返回值类型:System.Void
名称:get_Id
签名:Int32 get_Id()
属性:PrivateScope, Public, HideBySig, SpecialName
返回值类型:System.Int32
名称:set_Name
签名:Void set_Name(System.String)
属性:PrivateScope, Public, HideBySig, SpecialName
返回值类型:System.Void
名称:get_Name
签名:System.String get_Name()
属性:PrivateScope, Public, HideBySig, SpecialName
返回值类型:System.String
名称:set_Age
签名:Void set_Age(Int32)
属性:PrivateScope, Public, HideBySig, SpecialName
返回值类型:System.Void
名称:get_Age
签名:Int32 get_Age()
属性:PrivateScope, Public, HideBySig, SpecialName
返回值类型:System.Int32
名称:ToString
签名:System.String ToString()
属性:PrivateScope, Public, Virtual, HideBySig, VtableLayoutMask
返回值类型:System.String
名称:Equals
签名:Boolean Equals(System.Object)
属性:PrivateScope, Public, Virtual, HideBySig, VtableLayoutMask
返回值类型:System.Boolean
名称:GetHashCode
签名:Int32 GetHashCode()
属性:PrivateScope, Public, Virtual, HideBySig, VtableLayoutMask
返回值类型:System.Int32
名称:GetType
签名:System.Type GetType()
属性:PrivateScope, Public, HideBySig
返回值类型:System.Type
5.ConstructorInfo类型和EventInfo类型