Reflection first Understand
One: System. Type
Get basic information:
Type.Name//Class name
Type.fullname//full path
Type.namespace//Space name
Public classStudent { Public intId {Set;Get; } Public stringName {Set;Get; } Public intAge {Set;Get; } } Static voidMain (string[] args) {Student S=NewStudent () {Id =1, Name ="Zzj", age = A }; Type T=typeof(student); //View class InformationConsole.WriteLine ("name={0},fullname={1},namespace={2}", T.name, T.fullname, t.namespace); }View Code
:
Name=student
Fullname=win32. Reflection +student
Namespace=win32
Two. A few important methods under reflection
System.Reflection.
PropertyInfo: Encapsulation Type property Information
ConstructorInfo: Type constructors
MethodInfo: Facet names for types
EventInfo: Type event information
ParameterInfo: Method, constructor information
MemberInfo: Member Type Type.getmembers ()/getmember ()/findmember ()
1. Member Information and MemberInfo
StringBuilder SB =NewStringBuilder (); Memberinfo[] Me= T.getmembers ();//Get page MemberSb. Append ("View Type"+ t.name + t.name +"the member Information"); foreach(MemberInfo minchme) {sb. Append ("member"+m.tostring (). PadRight ( -)+"Type:"+m.membertype+"\ n"); } Console.WriteLine (SB);View Code
View member information for type studentstudent member void set_id (Int32) Type: Method member Int32 get_id () Type: Metho D member void Set_name (System.String) Type: Method member System.String Get_name () Type: Metho D member void Set_age (Int32) Type: Method member Int32 Get_age () Type: Metho D member System.String ToString () Type: Method member Boolean Equals (System.Object) Type: Metho D member Int32 GetHashCode () Type: Method member System.Type GetType () Type: Metho D member void. ctor () Type: constructor member Int32 Id type: Property member System.String Name Type: Property member Int32 Age Type: Property member Win32. Reflection+student+Major type: NestedType please press any key to continue ...
View Code
2. Field information and FieldInfo type
If you want to get all the fields of a type, you can use the GetFields () method to add a method again
Fieldexplore (): (But the feeling can only get properties, and Static)
Static voidFieldexplor (Type t) {StringBuilder SB=NewStringBuilder (); Fieldinfo[] Fields=T.getfields (); Sb. Append ("View Type"+ T.name +"the field information: \ n"); Sb. Append (string. Empty.padleft ( -,'-') +"\ n"); foreach(FieldInfo fiinchFields ) {sb. Append ("Name:"+ FI. Name +"\ n"); Sb. Append ("Type:"+ FI. FieldType +"\ n"); Sb. Append ("Features:"+ FI. Attributes +"\ n"); } Console.WriteLine (sb.) ToString ()); }View Code
To view field information for type student:
--------------------------------------------------
Name: Love
Type: System.String
Features: Public
Name: Lv
Type: System.Int32
Features: Public, Static
3. Attribute information and PropertyInfo type
Static voidPropertydexplor (Type t) {StringBuilder SB=NewStringBuilder (); Propertyinfo[] Fields=t.getproperties (); Sb. Append ("View Type"+ T.name +"property information for: \ n"); Sb. Append (string. Empty.padleft ( -,'-') +"\ n"); foreach(PropertyInfo fiinchFields ) {sb. Append ("Name:"+ FI. Name +"\ n"); Sb. Append ("Type:"+ FI. PropertyType +"\ n"); Sb. Append ("Readable:"+ FI. CanRead +"\ n"); Sb. Append ("can write:"+ FI. CanWrite +"\ n"); Sb. Append ("Features:"+ FI. Attributes +"\ n"); } Console.WriteLine (sb.) ToString ()); }View Code
To view property information for type student:
--------------------------------------------------
Name: Id
Type: System.Int32
Readable: True
Writable: True
Feature: None
Names: Name
Type: System.String
Readable: True
Writable: True
Feature: None
Name: Age
Type: System.Int32
Readable: True
Writable: True
Feature: None
4. Method information and MethodInfo type
Static voidMethodexplore (Type t) {StringBuilder SB=NewStringBuilder (); Methodinfo[] Me= T.getmethods ();//Get page MemberSb. Append ("View Type"+ t.name + t.name +"the member Information"); foreach(MethodInfo minchme) {sb. Append ("Name:"+ M.name +"\ n"); Sb. Append ("Signature:"+ m.tostring () +"\ n"); Sb. Append ("Properties:"+ M.attributes +"\ n"); Sb. Append ("return value type:"+ M.returntype +"\ n"); } Console.WriteLine (SB); }View Code
View member information for type studentstudent name: set_id
Signature: Void set_id (Int32)
Properties: PrivateScope, Public, Hidebysig, SpecialName
Return value type: system.void
Name: get_id
Signature: Int32 get_id ()
Properties: PrivateScope, Public, Hidebysig, SpecialName
Return value type: System.Int32
Name: Set_name
Signature: Void Set_name (System.String)
Properties: PrivateScope, Public, Hidebysig, SpecialName
Return value type: system.void
Name: get_name
Signature: System.String get_name ()
Properties: PrivateScope, Public, Hidebysig, SpecialName
Return value type: System.String
Name: Set_age
Signature: Void set_age (Int32)
Properties: PrivateScope, Public, Hidebysig, SpecialName
Return value type: system.void
Name: Get_age
Signature: Int32 get_age ()
Properties: PrivateScope, Public, Hidebysig, SpecialName
Return value type: System.Int32
Name: ToString
Signature: System.String ToString ()
Properties: PrivateScope, Public, Virtual, Hidebysig, Vtablelayoutmask
Return value type: System.String
Name: Equals
Signature: Boolean Equals (System.Object)
Properties: PrivateScope, Public, Virtual, Hidebysig, Vtablelayoutmask
Return value type: System.Boolean
Name: GetHashCode
Signature: Int32 GetHashCode ()
Properties: PrivateScope, Public, Virtual, Hidebysig, Vtablelayoutmask
Return value type: System.Int32
Name: GetType
Signature: System.Type GetType ()
Properties: PrivateScope, Public, hidebysig
Return value type: System.Type
5.ConstructorInfo type and EventInfo type
Detailed Reflection->type.system