string assemblyName = this.txtAssemblyName.Text;
string className = this.txtClassName.Text;
System.Diagnostics.Debug.Assert(assemblyName != null && assemblyName != string.Empty);
System.Diagnostics.Debug.Assert(className != null && className != string.Empty);
System.Reflection.AssemblyName asmName = new System.Reflection.AssemblyName(assemblyName);
AssemblyBuilder asmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.Run);
ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule("MyModule", true);
TypeBuilder typeBuilder = modBuilder.DefineType(className);
typeBuilder.DefineField("myfield",typeof(string), System.Reflection.FieldAttributes.Public);
MethodBuilder methodBuilder = typeBuilder.DefineMethod("CallMethod", System.Reflection.MethodAttributes.Public| System.Reflection.MethodAttributes.SpecialName | System.Reflection.MethodAttributes.HideBySig
, null, new Type[] { typeof(string) });
methodBuilder.DefineParameter(0, System.Reflection.ParameterAttributes.Retval, null);
methodBuilder.DefineParameter(1, System.Reflection.ParameterAttributes.In, "name");
ILGenerator ilGenerator = methodBuilder.GetILGenerator();
ilGenerator.Emit(OpCodes.Ldarg_1);
//System.Reflection.Assembly asmSystem = System.Reflection.Assembly.GetAssembly(typeof(MessageBox));
//Type typeMessageBox = asmSystem.GetType("MessageBox");
Type typeMessageBox = typeof(MessageBox);
System.Reflection.MethodInfo mInfoShow = typeMessageBox.GetMethod("Show",new Type[]{typeof(string)}
//System.Reflection.BindingFlags.Public| System.Reflection.BindingFlags.Static,
);
ilGenerator.EmitCall(OpCodes.Call, mInfoShow, null);
ilGenerator.Emit(OpCodes.Ret);
Type customType = typeBuilder.CreateType();
object customeObj = Activator.CreateInstance(customType); ;
System.Reflection.MethodInfo methodInfo = customType.GetMethod("CallMethod");
//customType.InvokeMember("CallMethod", System.Reflection.BindingFlags.Public, null, customeObj, new object[] { "123" });
//methodBuilder.Invoke(customeObj, new object[] { "123" });
methodInfo.Invoke(customeObj, new object[] { "11" });
Console.ReadLine();