c# - C# 2.0 中的扩展方法

标签 c# .net-2.0 extension-methods

我需要什么命名空间才能让我的扩展正常工作

这是我的扩展方法


using System;
using System.Collections.Generic;
using System.Web;
using System.Data;

namespace MyUtilities
{
    public static class DataReaderExtensions
    {
        public static DataTable ToDataTable(IDataReader reader)
        {
            DataTable table = new DataTable();
            table.Load(reader);
            return table;
        }
    }
}

当我尝试像这样使用它时

Session["Master"] = cust.GetCustomerOrderSummary((string)Session["CustomerNumber"]).ToDataTable();

这是行不通的。这是 .net 2.0

最佳答案

你不能。 C# 2.0 根本没有扩展方法。您可以在 Visual Studio 2008 中使用 C# 3.0 的扩展方法targeting .NET 2.0,如我的 "C#/.NET versions" article 中所述但您无法说服 C# 2.0 编译器表现得好像它了解什么是扩展方法。

关于c# - C# 2.0 中的扩展方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/707145/

相关文章:

c# - 创建扩展方法以将类型包装为 IEnumerables

c# - 扩展方法和静态方法有什么区别?

c# - 自动异步套接字测试

c# - 存储库模式中的可重用模型

c# - 在 .NET 2 中搜索具有特定属性值的节点的 XML 文件

c# - 为什么将 null 转换为原语(即 : int) in .net 2.0 会抛出 null ref 异常而不是无效转换异常?

c# - 如何在没有 SelectionStart 的情况下设置 TextBox 光标位置

c# - 如何附加到表达式

c# - 悬停 ComboBox 时更改 ComboBoxItem 的前景色

c#-4.0 - 使用 C# 扩展方法的 DTO 映射器