Reflection-implement simple method calls
1. What is reflection?
Reflection is to parse codes loaded into the runtime era of Public languages and dynamically access or modify some of these il codes.
You can use reflection to dynamically create instances of the type, bind the type to an existing object, or obtain the type from an existing object. Then, you can call methods of the type or access its fields and attributes.
A typical usage is to abstract the factory method. By specifying the assembly or namespace specifications in the configuration file, you can then access the specified assembly in the program and build the desired object.
2. How to Use reflection
To use reflection skillfully, you must. reflection has a certain understanding. In this Assembly, some column methods such as assembly, methodinfo, and eventinfo are provided to parse the loaded program, in addition, you need to have a certain understanding of the composition of msil. Reflection is nothing more than dynamic operations on programs that have been loaded or can be accessed.
3 Example /// test object for binding
Namespace simple_type
{
Public class mysimpleclass
{
Public void mymethod (string STR, int I)
{
Console. writeline ("mymethod parameters: {0}, {1}", STR, I );
}
Public void mymethod (string STR, int I, Int J)
{
Console. writeline ("mymethod parameters: {0}, {1}, {2}", STR, I, j );
}
}
}
// The main body of the running program
Namespace custom_binder
{
Using system;
Using system. reflection;
Using system. Globalization;
Using simple_type;
Class mymainclass
{
Static void main ()
{
// Obtain the mysimpleclass type
Type mytype = typeof (mysimpleclass );
// Create a mysimpleclass test instance
Mysimpleclass myinstance = new mysimpleclass ();
// Creates an object that inherits from system. reflection. Binder. This object implements the abstract method of binder,
// Used to specify how to match the real participation Parameter
Mycustombinder = new mycustombinder ();
// Obtain the mymethord method of the test object using the specified search method, and use the constructed mycustombinder to match the Parameter
Methodinfo mymethod = mytype. getmethod ("mymethod ",
Bindingflags. Public | bindingflags. instance,
Mycustombinder, new type [] {typeof (string ),
Typeof (INT)}, null );
Console. writeline (mymethod. tostring ());
// Call the method found based on the preceding conditions
Mytype. invokemember ("mymethod", bindingflags. invokemethod,
Mycustombinder, myinstance,
New object [] {"testing", (INT) 32 });
// The following is the auto-increment dynamic reflection method. The most obvious difference is that the method used to create an instance of the test object is different.
// You have not used the specified method to search for matching methods. Note that in this case, if function overloading exists,
// If you continue using the invokemember method without specifying the binder object, an exception may occur.
Object [] ARGs;
ARGs = new object [] {"str", 100 };
Object OBJ = assembly. getentryassembly (). createinstance ("simple_type.mysimpleclass ");
Methodinfo me = mytype. getmethod ("mymethod", new type [] {typeof (string), typeof (INT )});
Me. Invoke (OBJ, argS );
Console. readkey ();
}
}
}
// The following is part of the code that inherits system. reflection. Binder. For specific parameters and usage, refer to msdn
Class mycustombinder: binder
{
Public override methodbase bindtomethod (
Bindingflags bindingattr,
Methodbase [] match,
Ref object [] ARGs,
Parametermodifier [] modifiers,
Cultureinfo culture,
String [] names,
Out object state)
{
If (match = NULL)
Throw new argumentnullexception ("match ");
// Arguments are not being reordered.
State = NULL;
// Find a Parameter Match and return the first method
// Parameters that match the request.
Foreach (methodbase MB in match)
{
Parameterinfo [] parameters = Mb. getparameters ();
If (parametersmatch (parameters, argS ))
Return MB;
}
Return NULL;
}
}