Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试创建新类型时出现InvalidProgrameException_C#_.net_Reflection_Types_Reflection.emit - Fatal编程技术网

C# 尝试创建新类型时出现InvalidProgrameException

C# 尝试创建新类型时出现InvalidProgrameException,c#,.net,reflection,types,reflection.emit,C#,.net,Reflection,Types,Reflection.emit,我有以下代码: AssemblyBuilder newAssembly = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("CustomAssembly"), AssemblyBuilderAccess.Run); ModuleBuilder newModule = newAssembly.DefineDynamicModule("CustomModule"); TypeBuilder newType = newModule.Def

我有以下代码:

AssemblyBuilder newAssembly = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("CustomAssembly"), AssemblyBuilderAccess.Run);
ModuleBuilder newModule = newAssembly.DefineDynamicModule("CustomModule");
TypeBuilder newType = newModule.DefineType("CustomType", TypeAttributes.Public);
MethodBuilder newMethod = newType.DefineMethod("GetMessage", MethodAttributes.Public, typeof(string), Type.EmptyTypes);
byte[] methodBody = ((Func<string>)(() => "Hello, world!")).GetMethodInfo().GetMethodBody().GetILAsByteArray();
newMethod.CreateMethodBody(methodBody, methodBody.Length);
Type customType = newType.CreateType();
dynamic myObject = Activator.CreateInstance(customType);
string message = myObject.GetMessage();
AssemblyBuilder newAssembly=AssemblyBuilder.DefinedDynamicAssembly(新的AssemblyName(“CustomAssembly”),AssemblyBuilderAccess.Run);
ModuleBuilder newModule=newAssembly.DefinedDynamicModule(“CustomModule”);
TypeBuilder newType=newModule.DefineType(“CustomType”,TypeAttributes.Public);
MethodBuilder newMethod=newType.DefineMethod(“GetMessage”,MethodAttributes.Public,typeof(string),Type.EmptyTypes);
字节[]methodBody=((Func)(()=>“你好,世界!”).GetMethodInfo().GetMethodBody().GetILAsByteArray();
newMethod.CreateMethodBody(methodBody,methodBody.Length);
类型customType=newType.CreateType();
动态myObject=Activator.CreateInstance(customType);
字符串消息=myObject.GetMessage();
但是,尝试调用
myObject.GetMessage()
时,在最后一行引发异常:

InvalidProgrameException-公共语言运行库检测到无效程序


我的代码出了什么问题,为什么会抛出此异常?

如果我不得不冒险猜测,我会说这是因为这一行:

byte[] methodBody = ((Func<string>)(() => "Hello, world!")).GetMethodInfo().GetMethodBody().GetILAsByteArray();

问题在于lambda表达式包含一个字符串,在编译时该字符串不是以方法体结束,而是以类型的元数据结束。lambda中的
ldstr
指令通过元数据标记引用字符串。当您获取IL字节并复制到新方法时,新方法中的
ldstr
将具有无效的元数据标记

AssemblyBuilder newAssembly = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("CustomAssembly"), AssemblyBuilderAccess.Run);
ModuleBuilder newModule = newAssembly.DefineDynamicModule("CustomModule");
TypeBuilder newType = newModule.DefineType("CustomType", TypeAttributes.Public);
MethodBuilder newMethod = newType.DefineMethod("GetMessage", MethodAttributes.Public, typeof(string), Type.EmptyTypes);

var il = newMethod.GetILGenerator();
// return "Hello, world!";
il.Emit( OpCodes.Ldstr, "Hello, world!" );
il.Emit( OpCodes.Ret );

Type customType = newType.CreateType();
dynamic myObject = Activator.CreateInstance(customType);
string message = myObject.GetMessage();