在C# 2.0版本中使用Extensions方法
2011-09-06 18:29
375 查看
在C# 2.0版本中使用Extensions方法
最近在测试一个ftp类时发现其在.netcf V2版本下无法编译,但是却可以编译在3.0版本中。报错地方如下:
后来经过查找资料了解到是.netcf V3.0版本中的Extension Method特性。Extension Method 的一个主要用途便是构造辅助方法。
解决该问题可以使用以下两种方式:
方法一:在现有项目中添加System.core.dll的References。这样在编译时不会报错,并且经测试使用正常。但是每个项目中都会包含一个System.core.dll文件比较不爽。
方法二:在项目中添加以下代码:
添加该代码后编译不会报错,并且运行正常。也不用再添加那个.netcf V3.0的system.core.dll文件。
在查资料过程中发现“善用 C# 3.0 Extensions
方法”这篇文章不错。
最近在测试一个ftp类时发现其在.netcf V2版本下无法编译,但是却可以编译在3.0版本中。报错地方如下:
using System; using System.Runtime.CompilerServices; public static class Extensions { public static DateTime? ToDateTime(this WINAPI.FILETIME time) { if ((time.dwHighDateTime == 0) && (time.dwLowDateTime == 0)) { return null; } uint dwLowDateTime = (uint)time.dwLowDateTime; long fileTime = (time.dwHighDateTime << 0x20) | dwLowDateTime; return new DateTime?(DateTime.FromFileTimeUtc(fileTime)); } }
后来经过查找资料了解到是.netcf V3.0版本中的Extension Method特性。Extension Method 的一个主要用途便是构造辅助方法。
解决该问题可以使用以下两种方式:
方法一:在现有项目中添加System.core.dll的References。这样在编译时不会报错,并且经测试使用正常。但是每个项目中都会包含一个System.core.dll文件比较不爽。
方法二:在项目中添加以下代码:
namespace System.Runtime.CompilerServices { [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)] public sealed class ExtensionAttribute : Attribute { } }
添加该代码后编译不会报错,并且运行正常。也不用再添加那个.netcf V3.0的system.core.dll文件。
在查资料过程中发现“善用 C# 3.0 Extensions
方法”这篇文章不错。
相关文章推荐
- NUnit2.0详细使用方法(最新版本2.4.6)
- [转]C# 2.0:使用匿名方法、迭代程序和局部类来创建优雅的代码
- 【转】利用C#创建 IIS 站点并设置.NET Framework版本为ASP.NET 2.0 的方法(二) 转自孟子E章
- asp.net c#中使用FCKeditor的方法,版本2.66
- C# 2.0:使用匿名方法、迭代程序和局部类来创建优雅的代码
- 简介 WatiN是一个开源的用于Web测试自动化的类库,从watir中获得的灵感,使用C#开发。WatiN通过与浏览器的交互来实现自动化,使用起来具有轻便,简单的特点。目前最新版本为2.0,加入了对
- 利用C#创建 IIS 站点并设置.NET Framework版本为ASP.NET 2.0 的方法(一)
- C# 2.0:使用匿名方法、迭代程序和局部类来创建优雅的代码
- ASP.NET Core2.0 使用VSCode 版本1.13.1工具 开发控制台运行出现中文乱码问题及解决方法
- 利用C#创建 IIS 站点并设置.NET Framework版本为ASP.NET 2.0 的方法(二)
- FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用方法
- 利用C#创建 IIS 站点并设置.NET Framework版本为ASP.NET 2.0 的方法(转)
- 利用C#创建 IIS 站点并设置.NET Framework版本为ASP.NET 2.0 的方法(一)
- 利用C#创建 IIS 站点并设置.NET Framework版本为ASP.NET 2.0 的方法
- One time Password的使用方法C#版本
- 使用LINQ读取RSS,比传统的C#2.0方法节省了很多代码
- 关于mysql 数据库使用spring 2.0低版本 JdbcTemplate.queryForList 查询结果别名不起作用的解决方法
- C# 2.0:使用匿名方法、迭代程序和局部类来创建优雅的代码
- FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用方法
- C# 2.0:使用匿名方法、迭代程序和局部类来创建优雅的代码