简体   繁体   English

在c#中扩展Enumerable类?

[英]Extending the Enumerable class in c#?

I have situation to extend the Enumerable class in c# to add the new Range method that accepts long parameters. 我有条件扩展c#中的Enumerable类来添加接受长参数的新Range方法。 I cannot define the method like this 我无法定义这样的方法

public static IEnumerable<long> Range(this Enumerable source, long start, long length)
{
    for (long i = start; i < length; i++)
    {
        yield return i;
    }
}

Since extension methods are accesible only through its objects. 由于扩展方法只能通过其对象访问。 And it gives me an error 它给了我一个错误

'System.Linq.Enumerable': static types cannot be used as parameters 'System.Linq.Enumerable':静态类型不能用作参数

Can someonce clarify me how to do this 我们可以澄清一下如何做到这一点

Note: I know we can easily solve this without extension methods, but i needed this Enumrable class. 注意:我知道我们可以在没有扩展方法的情况下轻松解决这个问题,但我需要这个Enumrable类。

You (like me) are looking for static extension methods: 你(像我一样)正在寻找静态扩展方法:

http://madprops.org/blog/static-extension-methods/ http://madprops.org/blog/static-extension-methods/

It's not possible in C#. 这在C#中是不可能的。 The closest alternative is to define another static class with a similar name (LongEnumerable?) and add your static method to that. 最接近的替代方法是定义另一个具有相似名称的静态类(LongEnumerable?)并将静态方法添加到该类中。

Extension methods can only be called on instances of a type, and since Enumerable is a static type there will never be any instances of it, which means that you can't extend it. 扩展方法只能在类型的实例上调用,并且由于Enumerable是静态类型,因此永远不会有任何实例,这意味着您无法扩展它。

It makes no sense to have your Range method as an extension on IEnumerable<T> either. Range方法作为IEnumerable<T>的扩展名也没有意义。 Your method just generates a sequence of long values, it doesn't need to extend any particular instance. 您的方法只生成一系列long值,它不需要扩展任何特定实例。

Use a standard static utility method instead: 改为使用标准的静态实用方法:

var example = EnumerableUtils.Range(0, long.MaxValue).Where(x => (x % 2) == 0);

// ...

public static class EnumerableUtils
{
    public static IEnumerable<long> Range(long start, long count)
    {
        for (long i = start; i < start + count; i++)
        {
            yield return i;
        }
    } 
}

Why do you want to extend System.Linq.Enumerable? 为什么要扩展System.Linq.Enumerable? This class uses Extension methods to extend OTHER types that implement IEnumerable. 此类使用Extension方法扩展实现IEnumerable的OTHER类型。

The result would be, that you'd call: 结果是,你打电话:

Enumerable.Range(long, long);

You'd rather extend the long class directly: 你宁愿直接延长长班:

public static IEnumerable<long> Range(this long source, long length)
{
    for (long i = source; i < length; i++)
    {
        yield return i;
    }
}

This way you can start with 这样你就可以开始了

foreach (long item in 10.Range(20)) { }

You can't extend the Enumerable class, since you don't have an Enumerable instance - it's a static class. 您无法扩展Enumerable类,因为您没有Enumerable实例 - 它是一个static类。 Extension methods only work on instances, they never work on the static class itself. 扩展方法仅适用于实例,它们从不在静态类本身上工作。

You're going to have to create your own utility class for that; 你将不得不为此创建自己的实用程序类; you can'd add static methods via extension methods. 你可以通过扩展方法添加静态方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM