C# 如何创建通用委托EndInvoke?

C# 如何创建通用委托EndInvoke?,c#,generics,delegates,iasyncresult,C#,Generics,Delegates,Iasyncresult,因此,我有以下几点: private delegate Foo1 GetFooAsync1(Foo1 foo1); private delegate Foo2 GetFooAsync2(Foo2 foo2); private delegate Foo3 GetFooAsync3(Foo3 foo3); private delegate Foo4 GetFooAsync4(Foo4 foo4); private FooAsync1 foo1; private FooAsync2 foo2; pr

因此,我有以下几点:

private delegate Foo1 GetFooAsync1(Foo1 foo1);
private delegate Foo2 GetFooAsync2(Foo2 foo2);
private delegate Foo3 GetFooAsync3(Foo3 foo3);
private delegate Foo4 GetFooAsync4(Foo4 foo4);

private FooAsync1 foo1;
private FooAsync2 foo2;
private FooAsync3 foo3;
private FooAsync4 foo4;
列表还在继续,然后在一个方法中,我不想在每个EndInvoke上放置一个try catch,因为有时它确实会抛出一个异常,但它不应该停止系统,并继续使用其他Foo。。如果每个人都有一个尝试捕捉,那么它会在方法中占用很多空间

有没有一种通用的方法来调用end invoke?这样我就可以返回预期的结果了

var result1 = foo1.EndInvoke(fooIAsyncResult);

为了以通用方式实现这一点,您需要声明一个覆盖EndInvoke的扩展方法,如下所示:

public static class DelegateExtensions
{
    public static TResult EndInvoke<TDelegate, TResult>(this TDelegate asyncCaller, IAsyncResult asyncResult) where TDelegate : System.Delegate
    {
        TResult result = default(TResult);

        try
        {
            result = asyncCaller.EndInvoke(asyncResult);
        }
        catch ( Exception ex)
        {
            LogExceptionMessageOrWhatever(ex.Message);
            throw;
        }

        return result;
    }
}
现在您可以像平常一样调用EndInvoke。框架将自动使用您的版本

private void Main()
{
    Foo1Result foo1 = null;

    var foo1Factory = new GetFooAsync<Foo1Result>(
        () =>
        {
            return new Foo1Result();
        });


    foo1Factory.BeginInvoke(
        callback: asyncResult =>
            {
                foo1 = foo1Factory.EndInvoke(asyncResult);
            },
            @object: null);
}

在.net Func和Action中有两个biltin delegate使用它覆盖90%的场景您可以传递lambda表达式我不明白为什么要编写以委托为参数的委托。我写了一个没有这种嵌套的解决方案。如果这没有帮助,用实际代码替换示例。
private void Main()
{
    Foo1Result foo1 = null;

    var foo1Factory = new GetFooAsync<Foo1Result>(
        () =>
        {
            return new Foo1Result();
        });


    foo1Factory.BeginInvoke(
        callback: asyncResult =>
            {
                foo1 = foo1Factory.EndInvoke(asyncResult);
            },
            @object: null);
}