Using system;
Using system. Data;
Using system. reflection;
Using system. reflection. emit;
Namespace consoleapplicationreflection
{
Class classbuilder
{
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main (string [] ARGs)
{
Try
{
Console. writeline ("Enter values :");
String numbers = console. Readline ();
String [] values = numbers. Split (',');
// Generate a type that contains a method (returnsum) and returns the result of adding two numbers.
Type clstype = createtype ("ourassembly", "ourmodule", "mathops", "returnsum ");
// Generate a mathops instance based on reflection
Object clstypeinstance = activator. createinstance (clstype );
// Returnsum)
Methodinfo MI = clstype. getmethod ("returnsum ");
// Array of parameters required by the Method
Object [] objs = new object [] {int32.parse (Values [0]. tostring (), int32.parse (Values [1]. tostring ())};
// Execution Method
Object OBJ = mi. Invoke (clstypeinstance, objs );
// The following two lines of code have the same result as the above four lines of code.
// Object clstypeinstance = activator. createinstance (clstype );
// Object OBJ = matw.sclass. invokemember ("returnsum", bindingflags. invokemethod, null, clstypeinstance, new object [] {int32.parse (Values [0]. tostring (), int32.parse (Values [1]. tostring ())});
Console. writeline ("sum: {0}", obj. tostring ());
}
Catch (exception ex)
{
Console. writeline ("an error occured: {0}", Ex. Message );
}
Console. Readline ();
}
/// <Summary>
/// Dynamically generate the Assembly and type
/// </Summary>
/// <Param name = "assemblyname"> Assembly name </param>
/// <Param name = "modulename"> Module name </param>
/// <Param name = "classname"> type name </param>
/// <Param name = "methodname"> method name </param>
/// <Returns> </returns>
Public static type createtype (string assemblyname, string modulename, string classname, string methodname)
{
Try
{
Assemblyname name = new assemblyname ();
Name. Name = assemblyname;
Appdomain domain = system. Threading. thread. getdomain ();
Assemblybuilder assbuilder = domain. definedynamicassembly (name, assemblybuilderaccess. runandsave );
Modulebuilder MB = assbuilder. definedynamicmodule (modulename );
Typebuilder theclass = Mb. definetype (classname, typeattributes. Public | typeattributes. Class );
// Method Return Value Type
Type rtntype = typeof (INT );
// Method parameter type
Type [] Param = new type [2];
Param [0] = typeof (system. int32 );
Param [1] = typeof (system. int32 );
// Define the method Signature
Methodbuilder method = theclass. definemethod (methodname, methodattributes. Public, rtntype, Param );
// Define the method entity
Ilgenerator Gen = method. getilgenerator ();
GEN. declarelocal (typeof (INT); //. Locals Init ([0] int32 C)
GEN. emit (Opcodes. ldarg_1); // il_0000: ldarg.1
GEN. emit (Opcodes. ldarg_2); // il_0001: ldarg.2
GEN. emit (Opcodes. Add); // il_0002: add
GEN. emit (Opcodes. stloc_0); // il_0003: stloc.0
GEN. emit (Opcodes. ldloc_0); // il_0004: ldloc.0
GEN. emit (Opcodes. Ret); // il_0005: Ret
// Return the generated type
Return theclass. createtype ();
}
Catch (exception ex)
{
Console. writeline ("an error occured: {0}", Ex. Message );
Return NULL;
}
}
}
}