Hi All,
I'm learning how to write C# using reflection, but don't know how to
code using reflection
this.Controls.A dd(this.label1) ;
Could anyone help, Thank in advance.
xxx============ =============== =============== =============== =============
using System;
using System.Reflecti on;
using System.Reflecti on.Emit; // To emit MSIL
using System.Threadin g; // To get Current AppDomain
using System.IO;
public class HelloWorld
{
static void Main()
{
// public class Form1:System.Wi ndows.Forms.For m{Form1(){}}
// Form1 s= new Form1();
Assembly WinFormasm = Assembly.Load(" System.Windows. Forms,
Version=1.0.500 0.0, Culture=neutral , PublicKeyToken= b77a5c561934e08 9");
Type WinForm = WinFormasm.GetT ype("System.Win dows.Forms.Form ");
// Create a simple name for the assembly.
AssemblyName assemblyName = new AssemblyName();
assemblyName.Na me = "Form1Assembly" ;
// Create the dynamic assembly.
AssemblyBuilder assembly =
Thread.GetDomai n().DefineDynam icAssembly(asse mblyName,
AssemblyBuilder Access.Run);
// Create a dynamic module.
ModuleBuilder module = assembly.Define DynamicModule(" Form1Module");
// Define a public class named "Form1Class " in the assembly.
TypeBuilder Form1Class = module.DefineTy pe("Form1",
TypeAttributes. Public, WinForm);
// Create the constructor.
Type[] constructorArgs = null;
ConstructorBuil der constructor =
Form1Class.Defi neConstructor(M ethodAttributes .Public,
CallingConventi ons.Standard, constructorArgs );
// Generate IL for the method. The constructor calls its superclass
// constructor. The constructor stores its argument in the private field.
ILGenerator constructorIL = constructor.Get ILGenerator();
constructorIL.E mit(OpCodes.Lda rg_0);
ConstructorInfo superConstructo r = WinForm.GetCons tructor(new Type[0]);
constructorIL.E mit(OpCodes.Cal l, superConstructo r);
constructorIL.E mit(OpCodes.Ret );
// the class CodeGenClass.
Type Form1 = Form1Class.Crea teType();
object s= Activator.Creat eInstance (Form1);
object label1= Activator.Creat eInstance
(WinFormasm.Get Type("System.Wi ndows.Forms.Lab el"));
object textBox1= Activator.Creat eInstance
(WinFormasm.Get Type("System.Wi ndows.Forms.Tex tBox"));
object button1= Activator.Creat eInstance
(WinFormasm.Get Type("System.Wi ndows.Forms.But ton"));
/* please show how to implement these 3 lines using reflection.
this.Controls.A dd(this.label1) ;
this.Controls.A dd(this.button1 );
this.Controls.A dd(this.textBox 1);
*/
Form1.GetProper ty("Text", typeof(string)) .SetValue(s, "Hello
World",null);
Console.WriteLi ne("value {0}",Form1.GetP roperty("Text",
typeof(string)) .GetValue(s, null));
// Application.Run (New Form1);
Type WinApplication =
WinFormasm.GetT ype("System.Win dows.Forms.Appl ication");
MethodInfo method = WinApplication. GetMethod("Run" , new
Type[]{WinForm});
object result = method.Invoke(n ull, new object[]{s});
}
}
I'm learning how to write C# using reflection, but don't know how to
code using reflection
this.Controls.A dd(this.label1) ;
Could anyone help, Thank in advance.
xxx============ =============== =============== =============== =============
using System;
using System.Reflecti on;
using System.Reflecti on.Emit; // To emit MSIL
using System.Threadin g; // To get Current AppDomain
using System.IO;
public class HelloWorld
{
static void Main()
{
// public class Form1:System.Wi ndows.Forms.For m{Form1(){}}
// Form1 s= new Form1();
Assembly WinFormasm = Assembly.Load(" System.Windows. Forms,
Version=1.0.500 0.0, Culture=neutral , PublicKeyToken= b77a5c561934e08 9");
Type WinForm = WinFormasm.GetT ype("System.Win dows.Forms.Form ");
// Create a simple name for the assembly.
AssemblyName assemblyName = new AssemblyName();
assemblyName.Na me = "Form1Assembly" ;
// Create the dynamic assembly.
AssemblyBuilder assembly =
Thread.GetDomai n().DefineDynam icAssembly(asse mblyName,
AssemblyBuilder Access.Run);
// Create a dynamic module.
ModuleBuilder module = assembly.Define DynamicModule(" Form1Module");
// Define a public class named "Form1Class " in the assembly.
TypeBuilder Form1Class = module.DefineTy pe("Form1",
TypeAttributes. Public, WinForm);
// Create the constructor.
Type[] constructorArgs = null;
ConstructorBuil der constructor =
Form1Class.Defi neConstructor(M ethodAttributes .Public,
CallingConventi ons.Standard, constructorArgs );
// Generate IL for the method. The constructor calls its superclass
// constructor. The constructor stores its argument in the private field.
ILGenerator constructorIL = constructor.Get ILGenerator();
constructorIL.E mit(OpCodes.Lda rg_0);
ConstructorInfo superConstructo r = WinForm.GetCons tructor(new Type[0]);
constructorIL.E mit(OpCodes.Cal l, superConstructo r);
constructorIL.E mit(OpCodes.Ret );
// the class CodeGenClass.
Type Form1 = Form1Class.Crea teType();
object s= Activator.Creat eInstance (Form1);
object label1= Activator.Creat eInstance
(WinFormasm.Get Type("System.Wi ndows.Forms.Lab el"));
object textBox1= Activator.Creat eInstance
(WinFormasm.Get Type("System.Wi ndows.Forms.Tex tBox"));
object button1= Activator.Creat eInstance
(WinFormasm.Get Type("System.Wi ndows.Forms.But ton"));
/* please show how to implement these 3 lines using reflection.
this.Controls.A dd(this.label1) ;
this.Controls.A dd(this.button1 );
this.Controls.A dd(this.textBox 1);
*/
Form1.GetProper ty("Text", typeof(string)) .SetValue(s, "Hello
World",null);
Console.WriteLi ne("value {0}",Form1.GetP roperty("Text",
typeof(string)) .GetValue(s, null));
// Application.Run (New Form1);
Type WinApplication =
WinFormasm.GetT ype("System.Win dows.Forms.Appl ication");
MethodInfo method = WinApplication. GetMethod("Run" , new
Type[]{WinForm});
object result = method.Invoke(n ull, new object[]{s});
}
}