C# 编译器警告 1685
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/602927/
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
C# Compiler Warning 1685
提问by Mike Hofer
So, (seemingly) out of the blue, my project starts getting compiler warning 1685:
因此,(似乎)出乎意料,我的项目开始收到编译器警告 1685:
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”的定义
Perplexed, I researched the MSDN article to figure out its cause. Here's the information I found:
困惑,我研究了 MSDN 文章以找出其原因。这是我找到的信息:
Visual C# Reference: Errors and Warnings Compiler Warning (level 1) CS1685
Error Message The predefined type 'System.type name' is defined in multiple assemblies in the global alias; using definition from 'File Name'
This error occurs when a predefined system type such as System.int32 is found in two assemblies. One way this can happen is if you are referencing mscorlib from two different places, such as trying to run the.Net Framework versions 1.0 and 1.1 side-by-side.
The compiler will use the definition from only one of the assemblies. The compiler searches only global aliases, does not search libraries defined /reference. If you have specified /nostdlib, the compiler will search for Object, and in the future start all searches for predefined types in the file where it found Object.
Visual C# 参考:错误和警告编译器警告(级别 1)CS1685
错误消息 在全局别名中的多个程序集中定义了预定义类型“System.type name”;使用“文件名”中的定义
当在两个程序集中找到预定义的系统类型(例如 System.int32)时,会发生此错误。发生这种情况的一种方式是,如果您从两个不同的地方引用 mscorlib,例如尝试并排运行 .Net Framework 1.0 和 1.1 版。
编译器将仅使用来自其中一个程序集的定义。编译器只搜索全局别名,不搜索定义在 /reference 的库。如果您指定了 /nostdlib,编译器将搜索 Object,并在以后在找到 Object 的文件中开始所有预定义类型的搜索。
Now I'm really scratching my head.
现在我真的在挠头。
I'm not running two different versions of the .NET Framework (unless you count 2.0 and 3.5).
I'm not referencing any bizarre assemblies that might make me suspicious.
I don't recall making any changes to my application that would spur this change.
I've verified that all components target .NET Framework version v2.0.50727.
我没有运行两个不同版本的 .NET Framework(除非你算上 2.0 和 3.5)。
我不是在引用任何可能让我产生怀疑的奇怪程序集。
我不记得对我的应用程序进行了任何会刺激这种变化的更改。
我已经验证所有组件都针对 .NET Framework 版本 v2.0.50727。
I'm open to suggestions, or ideas on how to correct this. I treat warnings as errors, and it's driving me crazy.
我对如何纠正这个问题的建议或想法持开放态度。我将警告视为错误,这让我发疯。
What really bugs me about it is that I don't know whyit's occurring. Things that happen should have a discernable cause, and I should know why they happened. If I can't explain it, I can't accurately remedy it. Guesswork is never satisfactory.
真正让我烦恼的是我不知道它为什么会发生。发生的事情应该有一个可辨别的原因,我应该知道它们为什么会发生。如果我不能解释它,我就无法准确地补救它。猜测永远不会令人满意。
The application is straightforward, consisting of a class library, and a windows forms application.
该应用程序很简单,由一个类库和一个 windows 窗体应用程序组成。
A C# class library DLL providing basic functionality encapsulating database access. This DLL references the following components:
- System
- System.Core
- System.Core.Data
- System.Data
- System.Data.DataSetExtensions
- System.Data.OracleClient
- System.Drawing
- System.Windows.Forms
- System.Xml
- System.Xml.Linq
A C# Windows Forms application providing the UI. This application references the following components:
- CleanCode
- CleanCodeControls (both of these provide syntax editor support, and are locally built against .NET 3.5).
- LinqBridge
- Roswell.Framework (the class library above)
- System
- System.Core
- System.Data
- System.Data.DataSetExtensions
- System.Data.OracleClient
- System.Deployment
- System.Design
- System.Drawing
- System.Windows.Forms
- System.Xml
- System.Xml.Linq
AC# 类库 DLL 提供封装数据库访问的基本功能。此 DLL 引用以下组件:
- 系统
- 系统核心
- 系统.核心.数据
- 系统数据
- System.Data.DataSetExtensions
- 系统.数据.OracleClient
- 系统图
- 系统.Windows.Forms
- 系统文件
- 系统文件.Linq
提供 UI 的 AC# Windows 窗体应用程序。此应用程序引用以下组件:
- 清洁代码
- CleanCodeControls(两者都提供语法编辑器支持,并且是针对 .NET 3.5 本地构建的)。
- 林克桥
- Roswell.Framework(上面的类库)
- 系统
- 系统核心
- 系统数据
- System.Data.DataSetExtensions
- 系统.数据.OracleClient
- 系统.部署
- 系统设计
- 系统图
- 系统.Windows.Forms
- 系统文件
- 系统文件.Linq
Let me know if you need further information and I'll gladly provide it.
如果您需要更多信息,请告诉我,我很乐意提供。
采纳答案by Marc Gravell
LINQBridge makes me immediately suspicious. The entire intent of this is to provide extension attribute/methods etc for 2.0 users. If you have 3.5 (System.Core.dll), don't use LINQBridge. If you doneed LINQBridge in 3.5 for some obscure reason (and I can't think of one), then you might have to use an extern alias. But I reallydoubt you need it!
LINQBridge 让我立刻怀疑起来。这样做的全部目的是为 2.0 用户提供扩展属性/方法等。如果您有 3.5 (System.Core.dll),请不要使用 LINQBridge。如果出于某种晦涩的原因(我想不出一个),在 3.5 中确实需要 LINQBridge,那么您可能必须使用 extern 别名。但我真的怀疑你需要它!
回答by JaredPar
Marc is almost certainly correct. Here's a way to verify
马克几乎肯定是正确的。这是验证的方法
- Open Reflector.exe
- Add all of Non-System assemblies
- F3 and search for ExtensionAttribute
- 打开反射器.exe
- 添加所有非系统程序集
- F3 并搜索 ExtensionAttribute
If it pops up anywhere besides System.Core then you know where it's coming from.
如果它出现在 System.Core 之外的任何地方,那么你就知道它来自哪里。
回答by VitalyB
Another solution for this issue is to use a global alias for the whole assembly:
此问题的另一个解决方案是对整个程序集使用全局别名:
Reference -> Properties -> Aliases -> Replace 'global' with something else
参考 -> 属性 -> 别名 -> 用其他东西替换 'global'
回答by Remco te Wierik
Another easy way to verify: In your code, temporarily use the class somewhere. Example:
另一种简单的验证方法:在您的代码中,暂时在某处使用该类。例子:
System.Runtime.CompilerServices.ExtensionAttribute x = null;
When building, this will generate error:
构建时,这将产生错误:
The type 'System.Runtime.CompilerServices.ExtensionAttribute' exists in both 'c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll' and .....
'System.Runtime.CompilerServices.ExtensionAttribute' 类型同时存在于 'c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll' 和 .....
And show you immediately the 2 sources causing the conflict.
并立即向您展示导致冲突的 2 个来源。
回答by Efrain
FYI: I had the same problem and was able to resolve it by using Resharper's "Optimize References" command, and then removing all unused references. Not completely sure whythat worked, but it did.
仅供参考:我遇到了同样的问题,并且能够通过使用 Resharper 的“优化引用”命令来解决它,然后删除所有未使用的引用。不完全确定为什么这有效,但确实如此。
回答by Cipher
Another solution for this issue => Right click project -> Properties -> Build -> Treat warnings as errors -> None
此问题的另一个解决方案 => 右键单击项目 -> 属性 -> 构建 -> 将警告视为错误 -> 无