简体   繁体   English

如何从非泛型函数返回泛型函数?

[英]How to return a generic function from a non-generic function?

This will probably be easiest to explain with an example. 用一个例子可能最容易解释。

So, let's start with the following TryNTimes function. 因此,让我们从下面的TryNTimes函数开始。

public static T TryNTimes<T>(Func<T> f, int n)
{
    var i = 0;
    while (true)
    {
        try
        {
            return f();
        }
        catch
        {
            if (++i == n)
            {
                throw;
            }
        }
    }
}

And use it like 并像这样使用

MyType x = TryNTimes(DoSomething, 3);
MyOtherType y = TryNTimes(DoSomethingElse, 3);

But I'm using this in many cases with the same N , so I'd like to make it easy to create a function that injects the n value into here. 但是我经常在N相同的情况下使用它,因此我想简化创建将n值注入此处的函数的过程。 So the use would be 所以用途是

var tryThreeTimes = CreateRetryWrapper(3);
MyType x = tryThreeTimes(DoSomething);
MyOtherType y = tryThreeTimes(DoSomethingElse);

The closest I could come up with was 我能想到的最接近的是

public static Func<Func<T>, T> CreateRetryWrapper<T>(int n)
{
    return f => TryNTimes(f, n);
}

But that's not really what I want, because it forces me to specify T a-priori, so it's not really reusable in the way that I want. 但这并不是我真正想要的,因为它迫使我指定T a-priori,因此它并不是真正按照我想要的方式可重用的。 I want to be able to delay the T , returning a generic function as a value. 我希望能够延迟T ,并返回一个通用函数作为值。 Something like 就像是

public static Func<Func<_>, _> CreateRetryWrapper(int n)
{
    return f => TryNTimes(f, n);
}

Is this something that's possible in C#? 这在C#中可能吗?

workaround: 解决方法:

class RetryWrapper 
{ 
    int n;
    public RetryWrapper(int _n) => n =_n;
    public T Try<T>(Func<T> f) => TryNTimes(f, n);
}

Use: 采用:

var tryThreeTimes = new RetryWrapper(3);
MyType x = tryThreeTimes.Try(DoSomething);
MyOtherType y = tryThreeTimes.Try(DoSomethingElse);
class RetryWrapper
{
    readonly int n;

    private RetryWrapper(int n)
    {
        this.n = n;
    }

    public static RetryWrapper Create(int n)
    {
        return new RetryWrapper(n);
    }

    public T TryNTimes<T>(Func<T> f)
    {
        var i = 0;
        while (true)
        {
            try
            {
                return f();
            }
            catch
            {
                if (++i == n)
                {
                    throw;
                }
            }
        }
    }
}

Usage: 用法:

RetryWrapper.Create(3).TryNTimes(() => 16);

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

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