我写了这段代码:
AssemblyName assemblyName = new AssemblyName("SamAsm");
AssemblyBuilder assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
TypeBuilder typeBuilder = assemblyBuilder.DefineDynamicModule("SamAsm.exe").DefineType("SamAsmType", TypeAttributes.Public);
MethodBuilder methodBuilder1 = typeBuilder.DefineMethod("Main", MethodAttributes.Static | MethodAttributes.Public, typeof(void), new Type[] { typeof(string) });
ILGenerator gen = methodBuilder1.GetILGenerator();
FieldInfo field1 = typeof(Form1).GetField("TextBox1", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
MethodInfo method2 = typeof(Control).GetProperty("Text", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).GetGetMethod();
MethodInfo method3 = typeof(String).GetMethod(
"op_Equality",
BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic,
null,
new Type[]
{
typeof(String),
typeof(String)
},
null
);
MethodInfo method4 = typeof(MessageBox).GetMethod(
"Show",
BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic,
null,
new Type[]
{
typeof(String)
},
null
);
LocalBuilder a = gen.DeclareLocal(typeof(Boolean));
System.Reflection.Emit.Label label42 = gen.DefineLabel();
gen.Emit(OpCodes.Nop);
gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Ldfld, field1);
gen.Emit(OpCodes.Callvirt, method2);
gen.Emit(OpCodes.Ldstr, "HI");
gen.Emit(OpCodes.Call, method3);
gen.Emit(OpCodes.Ldc_I4_0);
gen.Emit(OpCodes.Ceq);
gen.Emit(OpCodes.Stloc_0);
gen.Emit(OpCodes.Ldloc_0);
gen.Emit(OpCodes.Brtrue_S, label42);
gen.Emit(OpCodes.Nop);
gen.Emit(OpCodes.Ldstr, "You cracked me");
gen.Emit(OpCodes.Call, method4);
gen.Emit(OpCodes.Pop);
gen.Emit(OpCodes.Nop);
gen.MarkLabel(label42);
gen.Emit(OpCodes.Ret);
typeBuilder.CreateType().GetMethod("Main").Invoke(null, new string[] { null });
assemblyBuilder.SetEntryPoint(methodBuilder1, PEFileKinds.WindowApplication);}
当我运行它停止
typeBuilder.CreateType().GetMethod("Main").Invoke(null, new string[] { null });
出现此错误:
从呼叫目的地生成的异常。
我检查了发出的 IL 代码,我认为没问题。我应该怎么做才能解决这个问题?