C#2.0中的扩展方法 [英] Extension Method in C# 2.0

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

问题描述

要使用我的扩展名,我需要什么命名空间

What namespace do I need to get my extension to work

这是我的扩展名方法



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

it doesn't work. This is .net 2.0

推荐答案

您不能。 C#2.0根本没有扩展方法。您可以按照我的定位 .NET 2.0中使用C#3.0中的扩展方法。 noreferrer> C#/。NET版本文章,但是您不能说服C#2.0编译器像了解扩展方法一样工作。

You can't. C# 2.0 doesn't have extension methods at all. You can use extension methods from C# 3.0 in Visual Studio 2008 targeting .NET 2.0 as described in my "C#/.NET versions" article but you can't persuade a C# 2.0 compiler to act as if it understands what extension methods are.

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

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