vs2008中让基于.net 2.0的应用程序也可以使用扩展方法
2008-04-16 09:47
696 查看
使用过vs2008进行.net 3.0/3.5做过开发的都知道有一个很方面不修改现有代码的情况下扩展现有类库的编码方式-扩展方法;但使用他的前提是类库必须基于.net 3.0/3.5 framework。
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中申明
无意之中,在网上找到一篇帖子:Using Extension Methods in .net 2.0
只要添加以下代码就可以使编译通过并运行,但是必须使用.net 3.5的编译器。
由于没有合适的测试环境,至于是不是可以在只安装了.net 2.0的机器上运行就没有做测试了,如果哪位dx做了测试,欢迎告知!
Technorati : 扩展方法
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中申明
无意之中,在网上找到一篇帖子:Using Extension Methods in .net 2.0
只要添加以下代码就可以使编译通过并运行,但是必须使用.net 3.5的编译器。
namespace System.Runtime.CompilerServices { [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)] public sealed class ExtensionAttribute : Attribute { } }
由于没有合适的测试环境,至于是不是可以在只安装了.net 2.0的机器上运行就没有做测试了,如果哪位dx做了测试,欢迎告知!
Technorati : 扩展方法
相关文章推荐
- 在 .NET 2.0上使用“扩展方法”
- .Net 2.0中使用扩展方法
- 在.net 2.0/3.0程序中使用扩展方法
- 在.net 2.0/3.0程序中使用扩展方法
- 【翻译】使用VS2008开发运行在.net 2.0环境下的 ASP.NET AJAX 网站
- 在ASP.NET 4.0中使用.NET 2.0的请求验证行为方法(转)
- asp.net 2.0里也可以用JSON的使用方法
- VS2008 应用程序配置不正确,未能启动该应用程序。重新安装程序可以修复此问题。解决方法
- 扩展方法 -- 可以基于接口
- vs2008破解方法(试用期过了,可以破解继续使用)
- asp.net 2.0里也可以用JSON的使用方法
- 应用程序中的所有线程都可以访问方法中的公用字段。要同步对公用字段的访问,您可以使用属性替代字段,并使用 ReaderWriterLock 对象控制访问。为此,请按照下列步骤操作:
- QQ音乐(QQ MUSIC)使用代理的方法(可以扩展到所有无代理设置的软件)
- yii2.0 内部的可以使用的部分方法整理
- .NET 2.0 中使用Active Directory 应用程序模式 (ADAM)
- PHP7扩展开发之基于函数方式使用lib库的方法详解
- 在.NET 2.0应用程序中使用BackgroundWorker组件 实现进度条
- Winform跨线程操作控件,采用扩展方法适配.NET 2.0,C#封装委托方法
- 基于.NET 2.0的GIS开源项目SharpMap分析手记(六):SharpMap的功能扩展之路
- 在.NET中发送邮件,使用回执功能中遇到问题的解决方法[在NOTES服务器上,C#,.NET2.0环境]