Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute'

原因: 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属性中看不出来
解释: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, 问题复现。再次发布程序,问题解决。

原文链接:https://www.cnblogs.com/wxnew/p/4417347.html