使用C#扩展方法时出错 [英] Error when using extension methods in C#

查看:174
本文介绍了使用C#扩展方法时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到一个问题,让我觉得有一个在3.0框架的错误。当我尝试使用扩展方法我收到以下错误:

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 compiler required member
'System.Runtime.CompilerServices.ExtensionAttribute..ctor'

在使用这个简单的code:

When using this simple code:

public static class StringUtils {
    static void TestExtension(this String targetString) {

    }
}

,使这个编译错误消失,唯一的办法就是增加以下code:

The only way to make this compile error go away is to add the following code:

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

这是一个几个月,因为我已经使用扩展的方法,但我是pretty的肯定,我没有做到这一点。有没有其他人遇到这个问题?

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?

推荐答案

我只是碰到了这个问题我自己。 就我而言,那是因为我改装了VS 2005 / .NET 2.0的项目到VS 2008 / .NET 3.5的项目。该转换工具保持引用System.Core程序2.0,我无法找到一个简单的方法来改变引用System.Core程序3.5。

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 2008中的项目,它是适当的引用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

这篇关于使用C#扩展方法时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆