[TypeLoadException: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.]
   System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type) +0
   System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext) +180
   System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) +192
   System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg) +115
   System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent) +426
   System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeAssembly assembly, RuntimeType caType) +103
   System.Reflection.RuntimeAssembly.GetCustomAttributes(Type attributeType, Boolean inherit) +64
   WebActivator.AssemblyExtensions.GetActivationAttributes(Assembly assembly) +132
   WebActivator.ActivationManager.RunActivationMethods() +216
   WebActivator.ActivationManager.RunPreStartMethods() +43
   WebActivator.ActivationManager.Run() +69


原因: In .Net 4.5 the ExtensionAttribute class was moved from System.Core to mscorlib.
        ExtensionAttribute 类在4.5中从System.Core迁移到mscorlib中,项目运行时会按4.5的方法寻找导致异常。
       有人会说,项目都是按4.0编译的,4.5也卸载了,为什么还会出现此问题?原因在于引用的第三方类库可能还是关联的4.5版本,比如我遇到这个问题是由于Entityframework引用了4.5版本导致的,而4.0和4.5从dll属性中看不出来

参考:
http://www.cnblogs.com/gossip/p/3527970.html
http://stackoverflow.com/questions/13748055/could-not-load-type-system-runtime-compilerservices-extensionattribute-from-as
http://stackoverflow.com/questions/16039990/runtime-exception-with-net-4-5-build-could-not-load-type-system-runtime-compi



解释:System.Runtime.CompilerServices.ExtensionAttribute 在 .net 4.0里 在 System.Core.Dll里, 在 .net 4.5里则被移到了 mscorlib 中。
System.Core.Dll的版本显示的 4.0.0.0 版,但实际上已经被 .net 4.5替换,里面没有ExtensionAttribute 类。

未替换的版本在目录“C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0”下,用Reflector可以看到,里面有ExtensionAttribute类。



解决的方法:
解决方法1: 在用户的机器上安装 .net 4.5。 对OS的要求是:

Windows 7 SP1(x86 和 x64)
Windows Server 2008 R2 SP1 (x64)
Windows Server 2008 SP2(x86 和 x64)


解决方法2: 自己编译混淆的机器上,仅安装了.net 4.0, 未安装 4.5。

后记:把 4.5卸载后, 重新安装了 4.0, 问题复现。再次发布程序,问题解决。

 
posted on 2016-03-29 21:54  五星  阅读(4424)  评论(0编辑  收藏  举报