vs2008中让基于.net 2.0的应用程序使用扩展方法

 

namespace Tests

{
public static class ExtensionClass
{
public static void ToConsole(this string value)
{

System.Console.WriteLine(value);
}
}
class Program
{
static void Main()
{
string test = "Test String!";
test.ToConsole();
}
}
}

在vs2008中基于.net 2.0编译会报下面的编译错误

error CS1110: 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?

编译告诉我们说找不到System.Runtime.CompilerServices.ExtensionAttribute,这个类在System.Core.dll中申明

网上帖子:

只要添加以下代码就可以使编译通过并运行,但是必须使用.net 3.5的编译器

namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]
public sealed class ExtensionAttribute : Attribute { }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值