在 C# 中使用扩展方法时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/205644/
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
Error when using extension methods in C#
提问by Korbin
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:
我遇到了一个问题,让我认为 3.0 框架中存在错误。当我尝试使用扩展方法时,出现以下错误:
Missing compiler required member
'System.Runtime.CompilerServices.ExtensionAttribute..ctor'
When using this simple code:
使用这个简单的代码时:
public static class StringUtils {
static void TestExtension(this String targetString) {
}
}
The only way to make this compile error go away is to add the following code:
消除此编译错误的唯一方法是添加以下代码:
namespace System.Runtime.CompilerServices {
public class ExtensionAttribute : Attribute { }
}
It's been a few months since I have used extensions methods, but I'm pretty sure I didn't have to do this. Has anyone else come across this issue?
我已经有几个月没有使用扩展方法了,但我很确定我不必这样做。有没有其他人遇到过这个问题?
采纳答案by abelenky
I just ran into this problem myself. In my case, it was because I converted a VS 2005/.Net 2.0 project to a VS 2008/.Net 3.5 project. The conversion tool kept references to System.Core 2.0, and I couldn't find an easy way to change the references to System.Core 3.5.
我自己刚刚遇到了这个问题。就我而言,这是因为我将 VS 2005/.Net 2.0 项目转换为 VS 2008/.Net 3.5 项目。转换工具保留对 System.Core 2.0 的引用,我找不到一种简单的方法来更改对 System.Core 3.5 的引用。
I ended up re-creating the project in VS 2008 from scratch, and it was created with proper references to System.Core 3.5
我最终从头开始在 VS 2008 中重新创建了该项目,并且它是在正确引用 System.Core 3.5 的情况下创建的
回答by Tom Ritter
回答by Marc Gravell
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, LINQBridgedoes this.
您的目标是什么版本的 .NET?ExtensionAttribute 类在 System.Core.dll (.NET 3.5) 中提供,但如果您想在 .NET 2.0/3.0(显然是 C# 3.0)中使用扩展方法,您可以自己重新声明它。事实上,LINQBridge 就是这样做的。
[update] However, I'm slightly confused, because the error you shouldsee 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]
错误 1 无法定义新的扩展方法,因为找不到编译器所需的类型“System.Runtime.CompilerServices.ExtensionAttribute”。您是否缺少对 System.Core.dll 的引用?[剪断了一些路径的东西]
回答by Tamas Czinege
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.
扩展是在 C# 3.0 中引入的,另一方面,它是在 .NET 3.5 中引入的,因此您不能真正在 .NET 3.0 中直接使用它们。
回答by Gabriel Isenberg
Is this a web site project, by chance? Try changing the target framework from .NET 3.5 to an earlier version, and then back to .NET 3.5.
这是一个网站项目吗?尝试将目标框架从 .NET 3.5 更改为早期版本,然后再返回到 .NET 3.5。
回答by jyoung
A missing System.Core reference will give these symptoms.
缺少 System.Core 引用会出现这些症状。
回答by Michael Buen
in VS, click Project (next to File,Edit,View), select Properties
在 VS 中,单击 Project(在 File、Edit、View 旁边),选择 Properties
then in Application tab (you'll notice you're already in 3.5), select the Target Framework to 2.0, then compile (it will error). then put it back again to 3.5, then compile again, the error will disappear
然后在应用程序选项卡中(您会注意到您已经在 3.5 中),将目标框架选择为 2.0,然后编译(它会出错)。然后再放回3.5,然后再编译,错误就会消失
i think it is just a small glitch in Visual Studio, just fool the IDE :-)
我认为这只是 Visual Studio 中的一个小故障,只是愚弄了 IDE :-)
回答by Michael Buen
Try: Project, Add Reference, find System Core 3.5.0.0 in the list, and OK to add it.
尝试:Project,Add Reference,在列表中找到System Core 3.5.0.0,确定添加。
回答by lexx
This problem is indeed caused by an incorrect reference to version 2 of System.Core . This is normally caused when upgrading from an earlier version of .NET to .NET 3.5 . If it is a website that you are experiencing this problem in then it can be remedied by following the steps below:
此问题确实是由于对 System.Core 版本 2 的错误引用引起的。这通常是在从 .NET 的早期版本升级到 .NET 3.5 时引起的。如果您在某个网站上遇到此问题,则可以按照以下步骤进行修复:
1) In web.config add a reference to System.Core v3.5:
1) 在 web.config 中添加对 System.Core v3.5 的引用:
<assemblies>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
2) In web.config add the following as a child of configuration:
2) 在 web.config 中添加以下作为配置的子项:
<configuration>
<!--Some other config-->
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Core" publicKeyToken="B77A5C561934E089"/>
<bindingRedirect oldVersion="2.0.0.0-2.1.0.0" newVersion="3.5.0.0"/>
</dependentAssembly>
</assemblyBinding>
</configuration>
回答by Andrew Myhre
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.
我在从 VS 2008 升级到 VS 2010 Beta 2 的类库项目中遇到了同样的问题。直到升级后我才向项目添加任何扩展方法,然后我开始看到相同的错误。
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
在此博客上找到了提示:http: //blog.flexforcefive.com/?p=105