首页 新闻 会员 周边

动态编译程序集,取Attribute时报错

0
悬赏园豆:10 [已关闭问题] 关闭于 2014-07-28 23:01

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) });


t1.SetCustomAttribute(new CustomAttributeBuilder(con, new[] { t2.UnderlyingSystemType }));


var type1 = t1.CreateType();
var type2 = t2.CreateType();

Console.WriteLine(type1);
Console.WriteLine(type2);
Console.WriteLine(assembly.GetReferencedAssemblies());
Console.WriteLine(type1.GetCustomAttributes<MetadataTypeAttribute>());


最后一句报
System.IO.FileNotFoundException : 未能加载文件或程序集“dynamic, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项。系统找不到指定的文件。
在 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)
在 Fireasy.Common.Extensions.ReflectionExtension.GetCustomAttributes(ICustomAttributeProvider provider, Boolean inherit) 位置 ReflectionExtension.cs: line 70
在 Fireasy.Data.Test.Entity.EntityBuilderTests.Test2() 位置 EntityBuilderTests.cs: line 98

faib的主页 faib | 初学一级 | 园豆:167
提问于:2013-05-09 14:50
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册