请教一个动态编译程序集的问题,在线等啊

faib920 2013-05-08 09:06:41
我想使用emit动态编译以下两个类的程序集,该程序实际上并未保存成dll,以下这个是保存后的结构


但是将以上程序集引入程序后,在使用
var t = typeof(Orders_11);
Console.WriteLine(t.GetCustomAttributes(typeof(MetadataTypeAttribute), false));

时,提示
System.IO.FileLoadException : 未能加载文件或程序集“test9, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)
在 System.Reflection.CustomAttribute._CreateCaObject(RuntimeModule pModule, IRuntimeMethodInfo pCtor, Byte** ppBlob, Byte* pEndBlob, Int32* pcNamedArgs)
在 System.Reflection.CustomAttribute.CreateCaObject(RuntimeModule module, IRuntimeMethodInfo ctor, ref IntPtr blob, IntPtr blobEnd, ref Int32 namedArgs)
在 System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)
在 System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
在 System.RuntimeType.GetCustomAttributes(Type attributeType, Boolean inherit)


初步分析原因可能是 MetadataTypeAttribute 使用了同一程序集中的另一个类,这里换成 typeof(string) 就没有问题了,请高手指点,谢先!
...全文
414 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
faib920 2013-05-09
  • 打赏
  • 举报
回复
我最后顶一次,没人帮忙我就让它烂掉,从此不踏入csdn
faib920 2013-05-09
  • 打赏
  • 举报
回复
var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("dynamic"), AssemblyBuilderAccess.Run); var module = assembly.DefineDynamicModule("Main"); var t1 = module.DefineType("t1", TypeAttributes.Class | TypeAttributes.Public); var t2 = module.DefineType("t2", TypeAttributes.Class | TypeAttributes.Public); var con = typeof(MetadataTypeAttribute).GetConstructor(new [] { typeof(Type) }); var type1 = t1.CreateType(); var type2 = t2.CreateType(); t1.SetCustomAttribute(new CustomAttributeBuilder(con, new[] { t2.UnderlyingSystemType })); Console.WriteLine(type1); Console.WriteLine(type2); Console.WriteLine(assembly.GetReferencedAssemblies()); Console.WriteLine(type1.GetCustomAttributes<MetadataTypeAttribute>()); 最后一次报错
faib920 2013-05-08
  • 打赏
  • 举报
回复
使用DefineDynamicModule方法定义ModuleBuilder时,name是不是必须取Main

17,748

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧