Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.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#_.net_Delegates - Fatal编程技术网

C# 如何声明不同于函数签名的新委托?

C# 如何声明不同于函数签名的新委托?,c#,.net,delegates,C#,.net,Delegates,我正在尝试一个通用函数,如aaa,它是在代码中定义的。 第一个调用应更改b,因为它是按ref传递的,而第二个调用不应更改b,因为它是按val传递的。您不能对委托执行此操作,因为委托和aaa的参数不匹配。 您可以这样做(创建方法适配器aaaRefInt和aaaInt): 您的方法签名与代理的签名不匹配,这是一项要求。你能改变aaa的参数吗?@ChrisHardie我能改变aaa参数,但我需要一个通用函数,可以处理任意数量的参数。可能有100多名代表,他们每个人都有不同的签名!代表们的全部目的是使这

我正在尝试一个通用函数,如
aaa
,它是在代码中定义的。

第一个调用应更改
b
,因为它是按ref传递的,而第二个调用不应更改
b
,因为它是按val传递的。

您不能对委托执行此操作,因为委托和
aaa
的参数不匹配。
您可以这样做(创建方法适配器
aaaRefInt
aaaInt
):


您的方法签名与代理的签名不匹配,这是一项要求。你能改变aaa的参数吗?@ChrisHardie我能改变
aaa
参数,但我需要一个通用函数,可以处理任意数量的参数。可能有100多名代表,他们每个人都有不同的签名!代表们的全部目的是使这成为不可能,类型安全是一件大事。当您可以使用Action和Func泛型委托类型时,声明100个委托类型没有意义。@HansPassant我用它来在编码时获得参数名。。我需要VS弹出窗口来告诉我我的参数是什么,在这个例子中可以是
ref int first
。另一个原因是,我需要这个用于模糊处理类型…您试图挫败委托的全部目的。这不是它的本意。
delegate void EmptyBody(ref int first );
delegate void AnotherEmptyBody(int first );
delegate void AnotherEmptyBody1(int first, int second);

public void aaa(params object[] pars)
{
    DoSpecifiedProcessing();

    pars[0] = 11;
}

public bool OnInIt()
{
    int b = 0;

    b = 44;
    var n = new EmptyBody(aaa);
    n(ref b);
    //b variable must be 11


    b = 44;
    var x = new AnotherEmptyBody(aaa);
    x(b);
    //b shoudn't be changed in this call, so it should be 44
}
public void aaaRefInt(ref int first)
{
    object[] Args = new object[]{first};        
    aaa(Args);
    first = (int)Args[0];
}

public void aaaInt(int first)
{
    aaa(new object[]{first});
}

var n = new EmptyBody(aaaRefInt);
n(ref b);

var x = new AnotherEmptyBody(aaaInt);
x(b);