Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 方法返回委托,该委托乘以参数中委托的值_C#_Delegates - Fatal编程技术网

C# 方法返回委托,该委托乘以参数中委托的值

C# 方法返回委托,该委托乘以参数中委托的值,c#,delegates,C#,Delegates,我需要定义方法,该方法作为参数获取两个委托,并返回委托(它将乘以此委托从参数返回的值)。现在我有这样的东西,但我不能让它编译。你能给我一些建议或回答吗?我将非常感激 public Delegate MathP(Delegate mydelegate, Delegate mydelegate2) { return (Delegate) Delegate.CreateDelegate Delegate (int x, int y) { in

我需要定义方法,该方法作为参数获取两个委托,并返回委托(它将乘以此委托从参数返回的值)。现在我有这样的东西,但我不能让它编译。你能给我一些建议或回答吗?我将非常感激

public Delegate MathP(Delegate mydelegate, Delegate mydelegate2)
    {

        return (Delegate) Delegate.CreateDelegate Delegate (int x, int y) {
                 int results = (int)mydelegate.DynamicInvoke(x, y);
                 int results2 = (int)mydelegate2.DynamicInvoke(x, y);

                 return results* results2;
            };
    }

您可能最好使用表达式树。 此方法将产生您期望的结果:

static Delegate Combine(Delegate first, Delegate second)
{
    var firstParam = Expression.Parameter(typeof(int));
    var secondParam = Expression.Parameter(typeof(int));

    var expression = Expression.Lambda<Func<int, int, int>>(
        Expression.Multiply(
            Expression.Call(first.GetMethodInfo(), firstParam, secondParam),
            Expression.Call(second.GetMethodInfo(), firstParam, secondParam)),
        firstParam,
        secondParam);

    return expression.Compile();
}

您可能最好使用表达式树。 此方法将产生您期望的结果:

static Delegate Combine(Delegate first, Delegate second)
{
    var firstParam = Expression.Parameter(typeof(int));
    var secondParam = Expression.Parameter(typeof(int));

    var expression = Expression.Lambda<Func<int, int, int>>(
        Expression.Multiply(
            Expression.Call(first.GetMethodInfo(), firstParam, secondParam),
            Expression.Call(second.GetMethodInfo(), firstParam, secondParam)),
        firstParam,
        secondParam);

    return expression.Compile();
}

如果您可以将代理重写为
Func
,则执行以下操作相当容易:

public Func<int, int, int> MathP
    ( Func<int, int, int> mydelegate
    , Func<int, int, int> mydelegate2
    )
{
    return new Func<int, int, int>
        ( (x, y) => mydelegate(x, y) * mydelegate2(x, y)
        );
}
公共函数数学 (Func mydelegate) ,Func mydelegate2 ) { 返回新函数 ((x,y)=>mydelegate(x,y)*mydelegate2(x,y) ); }
如果您可以将代理重写为
Func
,则很容易做到:

public Func<int, int, int> MathP
    ( Func<int, int, int> mydelegate
    , Func<int, int, int> mydelegate2
    )
{
    return new Func<int, int, int>
        ( (x, y) => mydelegate(x, y) * mydelegate2(x, y)
        );
}
公共函数数学 (Func mydelegate) ,Func mydelegate2 ) { 返回新函数 ((x,y)=>mydelegate(x,y)*mydelegate2(x,y) ); }
是否可以声明所有委托实例都符合Func签名?@galenus是的,它们都符合Func签名。是否可以声明所有委托实例都符合Func签名?@galenus是的,它们都符合Func签名。