C# Compiler Warning 1685
Asked Answered
H

8

50

So, (seemingly) out of the blue, my project starts getting compiler warning 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'

Perplexed, I researched the MSDN article to figure out its cause. Here's the information I found:

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.

Now I'm really scratching my head.

  1. I'm not running two different versions of the .NET Framework (unless you count 2.0 and 3.5).

  2. I'm not referencing any bizarre assemblies that might make me suspicious.

  3. I don't recall making any changes to my application that would spur this change.

  4. I've verified that all components target .NET Framework version 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 why it'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.

  • 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

Let me know if you need further information and I'll gladly provide it.

Housekeeping answered 2/3, 2009 at 16:18 Comment(5)
see the following: #547319 suggest you close the question it's an exact duplicateAmharic
incidentally it is indeed LinqBridge which is causing this, you no longer need it at allAmharic
I did a search on the warning number and couldn't find any questions that contained it; my apologies for the duplicate. Incidentally, LinqBridge WAS, in fact, the problem. Thank you!Housekeeping
Good point. I have edited the linked question to contain it.Amharic
I triggered this error by installing IIS with .NET 3.5 instead of 4.5 by accident. Fix was to add 4.5 back in in "Add Features ..." in control panel.Involucre
D
22

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 do need 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 really doubt you need it!

Dianndianna answered 2/3, 2009 at 16:20 Comment(0)
A
104

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 .....

And show you immediately the 2 sources causing the conflict.

Aeolian answered 29/6, 2011 at 9:22 Comment(3)
Cool. In fact I didn't even need to build, as soon as I pasted that line into a code file, the intellisense warning told me all I needed to know!Roodepoortmaraisburg
This is a better general answer.Trug
Finally! A way to figure out what file is actually causing this warning.Scaphoid
D
22

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 do need 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 really doubt you need it!

Dianndianna answered 2/3, 2009 at 16:20 Comment(0)
E
22

Marc is almost certainly correct. Here's a way to verify

  1. Open Reflector.exe
  2. Add all of Non-System assemblies
  3. F3 and search for ExtensionAttribute

If it pops up anywhere besides System.Core then you know where it's coming from.

Escargot answered 2/3, 2009 at 16:32 Comment(2)
I just used grep -r ExtensionAttribute * in my project directory and found the culprit that way.Outwardly
@Outwardly how about windows ?Greiner
F
9

Another solution for this issue is to use a global alias for the whole assembly:

Reference -> Properties -> Aliases -> Replace 'global' with something else

Franko answered 9/11, 2010 at 13:48 Comment(0)
L
5

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 why that worked, but it did.

Lamdin answered 3/5, 2012 at 9:19 Comment(0)
P
0

My teams resolution for this CS1685 warning was removing the binding redirect for System.Text.Json.

<dependentAssembly>
   <assemblyIdentity name="System.Text.Json" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
   <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
</dependentAssembly>
Pyknic answered 16/2, 2023 at 15:26 Comment(0)
S
0

We got over 1000 such warnings. I believe it really slow down build performance. It would be nice if Microsoft could fix it or provide a workaround. Unfortunately the issue is logged but closed with a low priority :(

ASPNETCOMPILER : warning CS1685: The predefined type 'System.ObsoleteAttribute' is defined in multiple assemblies in the global alias; using definition from 'c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.8\mscorlib.dll'

Surge answered 7/9, 2023 at 18:25 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Reneerenegade
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewMaurilla
E
-4

Another solution for this issue => Right click project -> Properties -> Build -> Treat warnings as errors -> None

Echopraxia answered 8/1, 2015 at 14:13 Comment(1)
I would never allow this. Proper code has no warnings. This feels like sweeping it under the carpet. This warning can and therefore should be solved.Aeolian

© 2022 - 2024 — McMap. All rights reserved.