嗨,我写了以下代码:
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);}
当我尝试这个时,它阻止了我 gen.Emit(OpCodes.Ldfld,typeof(Form1).GetField("TextBox1", BindingFlags.Public | BindingFlags.NonPublic));
并告诉:该值不能为空。参数名称:con。有人可以帮助我吗?