我可以使用.NET 2.0或3.0的扩展方法和LINQ? [英] Can I use extension methods and LINQ in .NET 2.0 or 3.0?

查看:302
本文介绍了我可以使用.NET 2.0或3.0的扩展方法和LINQ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用.NET 2.0或3.0运行时添加一个扩展方法,我得到的错误:

When I try to add an extension method using the .NET 2.0 or 3.0 runtime, I get the error:

不能确定,因为所需的编译器一个新的扩展方法   类型System.Runtime.CompilerServices.ExtensionAttribute不能   找到。是否缺少引用System.Core.dll的?

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.Core程序可用引用的列表,当我尝试将其添加到项目中。什么我需要做的是能够使用扩展方法,进而 LINQ 在我的项目?

But I can't find System.Core in the list of available references when I try to add it to the project. What do I need to do to be able to use extension methods and in turn LINQ on in my projects?

推荐答案

扩展方法未添加到.NET,直到3.5。然而,这是不是更改了CLR,但更改该添加的编译器他们,所以你仍然可以使用他们在你的2.0和3.0的项目!唯一的要求是你必须有一个编译器,它可以创造3.5的项目才能够做到这一点的解决方法(视觉 &工作室NBSP; 2008及以上)。

Extension methods were not added to .NET until 3.5. However, it was not a change to the CLR, but a change to the compiler that added them, so you can still use them in your 2.0 and 3.0 projects! The only requirement is you must have a compiler that can create 3.5 projects to be able to do this workaround (Visual Studio 2008 and above).

当您尝试使用扩展方法是误导,因为你并不真正需要的 System.Core.dll的来使用扩展方法,你得到的错误。当您使用扩展方法,在幕后,编译器添加<一个href="http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.extensionattribute.aspx"相对=nofollow> [扩展] 属性的功能。如果你有一个编译器能够理解做什么用的 [扩展] 属性,你可以用它在你的2.0和3.0项目,如果你自己创建属性。

The error you get when you attempt to use an extension method is misleading as you do not truly need System.Core.dll to use extension methods. When you use a extension method, behind the scenes, the compiler is adding the [Extension] attribute to the function. If you have a compiler that understands what to do with the [Extension] attribute you can use it in your 2.0 and 3.0 projects if you create the attribute yourself.

只需添加下面的类到您的项目,然后就可以开始使用扩展方法:

Just add the following class to your project and you can then start using extension methods:

namespace System.Runtime.CompilerServices
{
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    public class ExtensionAttribute : Attribute
    {
    }
}

以上code块,坐在里面的 System.Core.dll的,所以这就是为什么错误说你需要包括DLL文件来使用它们。

The above code block is sitting inside System.Core.Dll, so that is why the error says you need to include the DLL file to use them.

现在,如果你想LINQ的功能,将采取一些额外的工作。您将需要重新实现扩展方法自己。为了模拟完整的 LINQ&NBSP;到&NBSP; SQL 功能code可以得到相当复杂。但是,如果你只是使用 LINQ&NBSP;到&NBSP;对象最LINQ方法并不复杂实施。这里有几个LINQ&NBSP;到&NBSP;从一个项目我写出来,让你开始对象替换功能

Now if you want LINQ functionality that will take a little extra work. You will need to re-implement the extension methods yourself. To mimic the full LINQ to SQL functionality the code can get quite complicated. However, if you are just using LINQ to Objects most LINQ methods are not complicated to implement. Here are a few LINQ to Objects replacement functions from a project I wrote out to get you started.

public static class LinqReplacement
{
    public delegate TResult Func<T, TResult>(T arg);
    public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);

    public static TSource First<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
    {
        if (source == null)
            throw new ArgumentNullException("source");
        if (predicate == null)
            throw new ArgumentNullException("predicate");

        foreach (TSource item in source)
        {
            if (predicate(item) == true)
                return item;
        }

        throw new InvalidOperationException("No item satisfied the predicate or the source collection was empty.");
    }

    public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source)
    {
        if (source == null)
            throw new ArgumentNullException("source");

        foreach (TSource item in source)
        {
            return item;
        }

        return default(TSource);
    }

    public static IEnumerable<TResult> Cast<TResult>(this IEnumerable source)
    {
        foreach (object item in source)
        {
            yield return (TResult)item;
        }
    }

    public static IEnumerable<TResult> SelectMany<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> selector)
    {
        if (source == null)
            throw new ArgumentNullException("source");
        if (selector == null)
            throw new ArgumentNullException("selector");

        foreach (TSource item in source)
        {
            foreach (TResult subItem in selector(item))
            {
                yield return subItem;
            }
        }
    }

    public static int Count<TSource>(this IEnumerable<TSource> source)
    {
        var asCollection = source as ICollection;
        if(asCollection != null)
        {
            return asCollection.Count;
        }

        int count = 0;
        foreach (TSource item in source)
        {
            checked //If we are counting a larger than int.MaxValue enumerable this will cause a OverflowException to happen when the counter wraps around.
            {
                count++;
            }
        }
        return count;
    }
}

与完全重新实行LINQ&NBSP的图书馆;到&NBSP;使用对象的 ExtensionAttribute 已加入可在的 LinqBridge 项目(感谢阿隆Guralnek )。

A library with the full re-implemenation of LINQ to Objects with the ExtensionAttribute already added in can be found in the LinqBridge project (Thanks Allon Guralnek).

这篇关于我可以使用.NET 2.0或3.0的扩展方法和LINQ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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