To use the C # reflection technology, you must first introduce the system. Reflection namespace. The classes in this namespace are dynamically loaded.ProgramSet, type, dynamic call method, setting and obtaining the value of attributes and fields, and the ability to obtain information about types and methods.
To dynamically assign values or values to attributes or fields of A type instance, you must first obtain the instance or type. Microsoft has provided enough methods for us.
1 classmyclass 2 { 3 Privateintfield; 4 Publicintfield 5 { 6 Get 7 { 8 Returnthis. field; 9 } 10 Set 11 { 12 This . Field = Value; 13 } 14 } 15 }
If there is an instance of this type:
Myclass myobj = new myclass ();
To dynamically assign values to the attribute field of this instance, you must first obtain the instance type:
Typet = typeof (myclass); another method is:
Typet = myobj. GetType (); as long as we get the object type, we can use reflection to "do whatever we want" on this object, haha.
T. getproperty ("field"). setvalue (myobj, 1, null); in this way, the attribute field in the object is assigned a value. If you write the attribute name and value to the configuration file, we can dynamically assign values to the attribute during the program running.
How to obtain attribute values using reflection:
Intpropvalue = convert. toint32 (T. getproperty ("field "). setvalue (myobj, null); well, it's time to dynamically assign values to object attributes using reflection.
(C #) Use reflection to dynamically call class members
To use reflection to dynamically call class members, you need a method of the type class: invokemember. The statement for this method is as follows:
Public ObjectInvokemember (StringName, bindingflags invokeattr, binder,ObjectTarget,Object[] ARGs );
Parameters
Name
String, which contains the name of the constructor, method, attribute, or field member to be called.
-Or-
An empty string ("") indicates the default Member of the call.
Invokeattr
One-bit blocking is composed of one or more bindingflags that specify the search execution method. Access can be one of bindingflags, such as public, nonpublic, Private, invokemethod, and getfield. You do not need to specify the search type. If the search type is omitted, bindingflags. Public | bindingflags. instance will be applied.
Binder
A binder object that defines a set of attributes and enables binding. Binding may involve selecting overload methods, mandatory parameter types, and calling members through reflection.
-Or-
If it is a null reference (nothing in Visual Basic), defaultbinder is used.
Target
The object on which the specified Member is to be called.
ARGs
An array containing parameters passed to the member to be called.
Return Value
Indicates the object returned by the called member.
Note:
The following bindingflags filter flag can be used to define members included in the search:
To obtain the returned value, bindingflags. instance or bindingflags. Static must be specified.
Specify bindingflags. Public to include public members in the search.
Specify bindingflags. nonpublic to include non-public members (Private Members and protected members) in the search ).
Specify bindingflags. flattenhierarchy to include static members in the hierarchy.
The following bindingflags modifier can be used to change the search execution method:
Bindingflags. ignorecase, indicating that the name case is ignored.
Bindingflags. declaredonly: searches for members declared on the type, rather than simply inherited members.
The following bindingflags call flag can be used to indicate the operations to be performed on members:
Createinstance, indicating to call the constructor. Ignore name. Invalid for other call flag.
Invokemethod indicates that a method is called, instead of a constructor or an initial value of the type. It is invalid for setfield or setproperty.
Getfield indicates obtaining the field value. It is invalid for setfield.
Setfield, indicating to set the field value. Invalid for getfield.
Getproperty indicates obtaining the property. It is invalid for setproperty.
Setproperty indicates setting properties. Invalid for getproperty.
The following example is a simple application of this method (I always thought that the simpler the example, the more intuitive the better, to make it easier to understand the meaning and function of the text .)
1 Using System; 2 Using System. reflection; 3 & NB 4 Namespace Leleapplication9 5 { 6 Class Love 7 { 8 Public Int Field1; 9 Private String _ Name; 10 Public Love () 11 { 12 } 13 Public String Name 14 { 15 Get 16 { 17 Return _ Name; 18 } 19 Set 20 { 21 _ Name = Value; 22 } 23 } 24 Public Int Getint ( Int A) 25 { 26 Return A; 27 } 28 Public Void Display ( String Str) 29 { 30 Console. writeline (STR ); 31 } 32 } 33 /// <Summary> 34 /// Class1. 35 /// </Summary> 36 Class Class1 37 { 38 /// <Summary> 39 /// The main entry point of the application. 40 /// </Summary> 41 [Stathread] 42 Static Void Main ( String [] ARGs) 43 { 44 // 45 // Todo: addCodeTo start the application 46 // 47 Love = New Love (); 48 Type type = Love. GetType (); 49 Object OBJ = type. invokemember ( Null , 50 Bindingflags. declaredonly | 51 Bindingflags. Public | bindingflags. nonpublic | 52 Bindingflags. instance | bindingflags. createinstance, Null , Null , ArgS ); 53 // Call a method without a return value 54 Type. invokemember ( " Display " , Bindingflags. invokemethod | bindingflags. Public | bindingflags. instance, Null , OBJ, New Object [] { " Aldfjdlf " }); 55 // Call methods with returned values 56 Int I = ( Int ) Type. invokemember ( " Getint " , Bindingflags. invokemethod | bindingflags # 0000cc " >. Public | bindingflags. instance, null, OBJ, new object [] {1 }); 57 Console. writeline (I ); 58 // Set attribute values 59 Type. invokemember ( " Name " , Bindingflags. setproperty, Null , OBJ, New String [] { " ABC " }); 60 // Get Attribute Value 61 String STR = ( String ) Type. invokemember ( " Name " , Bindingflags. getproperty, Null , OBJ, Null ); 62 Console. writeline (STR ); 63 // Set field value 64 Type. invokemember ( " Field1 " , Bindingflags. setfield, Null , OBJ, New Object [] { 444 }); 65 // Get Field Value 66 Int F = (Int ) Type. invokemember ( " Field1 " , Bindingflags. getfield, Null , OBJ, Null ); 67 Console. writeline (f ); 68 Console. Readline (); 69 } 70 } 71 }