当前位置:编程学习 > C#/ASP.NET >>

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

我想使用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) 就没有问题了,请高手指点,谢先! emit --------------------编程问答-------------------- 使用DefineDynamicModule方法定义ModuleBuilder时,name是不是必须取Main --------------------编程问答-------------------- 我最后顶一次,没人帮忙我就让它烂掉,从此不踏入csdn --------------------编程问答-------------------- 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>());


最后一次报错
补充:.NET技术 ,  .NET Framework
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,