C# 关于 ExtensionAttribute 的奇怪警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/546819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
strange warning about ExtensionAttribute
提问by JoelFan
I'm getting a strange warning:
我收到一个奇怪的警告:
The predefined type 'System.Runtime.CompilerServices.ExtensionAttribute' is defined in multiple assemblies in the global alias; using definition from 'c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll'
预定义类型'System.Runtime.CompilerServices.ExtensionAttribute'在全局别名中的多个程序集中定义;使用来自“c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll”的定义
There is no line number given, so it's hard to figure out what it's on about.
没有给出行号,所以很难弄清楚它是关于什么的。
The compiler error code is CS1685
编译器错误代码是CS1685
采纳答案by ShuggyCoUk
Are you using someone's dll (or your own) which had implemented this attribute (with exactly the same name) itself as a means of using some c# 3.0 features on pre .Net 3.5 runtimes? (A common trick)
您是否正在使用某人的 dll(或您自己的),该 dll 本身已实现此属性(具有完全相同的名称)作为在 .Net 3.5 运行时之前使用某些 c# 3.0 功能的一种方式?(常见的套路)
This is the probable cause. Since it is using the correct one (the MS one in the GAC) this is not a problem though you should hunt down the other and remove it.
这是可能的原因。由于它使用的是正确的(GAC 中的 MS),这不是问题,但您应该寻找另一个并将其删除。
回答by David Basarab
The compiler does not know which System.Runtime.CompilerServices.ExtensionAttribute
编译器不知道哪个 System.Runtime.CompilerServices.ExtensionAttribute
So it is using the defination from c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll'
所以它使用 c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll' 中的定义
A .dll you are using might have the same extenstion.
您使用的 .dll 可能具有相同的扩展名。
回答by JaredPar
Expanding on ShuggyCoUk's (correct) answer
扩展 ShuggyCoUk 的(正确)答案
Truthfully it doesn't matter which version of the attribute is used (GAC, 3rd part, etc ...). All that matters is the C#/VB compiler can find some attribute with the correct name. The attribute serves no functional purpose in code. It exists purely to tell the Compiler "hey, this is an extension method".
说实话,使用哪个版本的属性并不重要(GAC、第 3 部分等......)。重要的是 C#/VB 编译器可以找到一些具有正确名称的属性。该属性在代码中没有任何功能用途。它的存在纯粹是为了告诉编译器“嘿,这是一个扩展方法”。
You can safely ignore this warning.
您可以放心地忽略此警告。
回答by Ron
I agree with ShuggyCoUk that the best course of action is to try to remove the offending dll. That may not be possible, though.
我同意 ShuggyCoUk 的最佳做法是尝试删除有问题的 dll。不过,这可能是不可能的。
Another way to resolve the ambiguity that the compiler is complaining about is to change the Alias of the referenced dll. In your project, in the References folder, if you click on a referenced dll you will see the Aliases property. By default, this is "global", which allows you to do things like "global::SomeNamespace.SomeType". You might simply be able to change the alias to something else.
解决编译器所抱怨的歧义的另一种方法是更改引用的 dll 的别名。在您的项目中,在 References 文件夹中,如果您单击引用的 dll,您将看到 Aliases 属性。默认情况下,这是“全局”,它允许您执行诸如“global::SomeNamespace.SomeType”之类的操作。您可能只需将别名更改为其他内容即可。
This fixed a problem I had where I needed to reference Microsoft.Scripting.Core.dll, but it contained some types that conflicted with mscorlib.dll. I changed the Aliases property to be "ThirdParty" instead of "global", and that fixed the warning.
这解决了我需要引用 Microsoft.Scripting.Core.dll 的问题,但它包含一些与 mscorlib.dll 冲突的类型。我将别名属性更改为“第三方”而不是“全局”,并修复了警告。
回答by Luiz Carlos Faria
I have the same problem.
我也有同样的问题。
In my case the problem was the assembly Mono.Cecil.
就我而言,问题是程序集 Mono.Cecil。
Migrating from local references to nuget, when i add NHibernate references the package automatically adds this reference.
从本地引用迁移到 nuget,当我添加 NHibernate 引用时,包会自动添加此引用。
This reference was removed, and compiled my project again.
此引用已删除,并再次编译我的项目。
Remove it and be happy!!
删除它并快乐!
This image was taken from ILSpy ( http://i.stack.imgur.com/Qyd5o.png)
此图片取自 ILSpy ( http://i.stack.imgur.com/Qyd5o.png)
回答by Ruskin
I triggered this error by installing IIS with .NET 3.5 instead of 4.5 by accident.
我通过安装带有 .NET 3.5 而不是 4.5 的 IIS 意外触发了这个错误。
Fix was to add 4.5 back in in "Add Features ..." in control panel.
修复是在控制面板的“添加功能...”中重新添加 4.5。