Error when using extension methods in C#

后端 未结 16 2756
夕颜
夕颜 2020-12-13 01:31

I came across an issue that makes me think there is bug in the 3.0 framework. When I try to use extension methods I get the following error:

Missing compile         


        
相关标签:
16条回答
  • 2020-12-13 02:02

    Just target your VS project to .NET framework 3.5 and above. Most probably you converted your project from a previous version.

    0 讨论(0)
  • 2020-12-13 02:07

    Try: Project, Add Reference, find System Core 3.5.0.0 in the list, and OK to add it.

    0 讨论(0)
  • 2020-12-13 02:10

    Extensions are introduced in C# 3.0, which on the other hand was introduced in .NET 3.5 so you can't really use them in .NET 3.0 directly.

    0 讨论(0)
  • 2020-12-13 02:12

    What version of .NET are you targetting? The ExtensionAttribute class is shipped in System.Core.dll (.NET 3.5), but you can re-declare it yourself if you want to use extension methods in .NET 2.0/3.0 (with C# 3.0, obviously). In fact, LINQBridge does this.

    [update] However, I'm slightly confused, because the error you should see is:

    Error 1 Cannot define a new extension method because the compiler required type 'System.Runtime.CompilerServices.ExtensionAttribute' cannot be found. Are you missing a reference to System.Core.dll? [snipped some path stuff]

    0 讨论(0)
  • 2020-12-13 02:14

    For ongoing reference:

    I have battled this exact same error message for the last two hours in the .Net 4.0 framework and finally tracked it down to a corrupted reference to the Microsoft.CSharp dll. It was such a frustratingly simple issue. I deleted the offending reference and added a "new" reference to that dll which was located at:

    C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Microsoft.CSharp.dll
    

    Hopefully this will save someone else the time and aggravation of tracking down a solution to this error message.

    0 讨论(0)
  • 2020-12-13 02:15

    I had the same issue in a class library project that I had upgraded from VS 2008 to VS 2010 Beta 2. I hadn't added any extension methods to the project until after the upgrade, then I started seeing the same error.

    Adding a class with the following code to the project solved the problem:

    namespace System.Runtime.CompilerServices
    {
        public class ExtensionAttribute : Attribute { }
    }
    

    Found the tip on this blog: http://blog.flexforcefive.com/?p=105

    0 讨论(0)
提交回复
热议问题