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

C# 如何将重载方法组作为单个参数传递?

C# 如何将重载方法组作为单个参数传递?,c#,C#,我知道我可以通过这样的方法: Class A: public void foo(Action<Class, Class, Class> bar) { Class a; Class b; Class c; bar(a, b, c); } Class B: public main() { foo(bar); } protected void bar(Class a, Class b, Class c); 如何做到这一点?除了以字符串形式按名称传递

我知道我可以通过这样的方法:

Class A:
public void foo(Action<Class, Class, Class> bar)
{
    Class a;
    Class b;
    Class c;
    bar(a, b, c);
}
Class B:
public main()
{
    foo(bar);
}
protected void bar(Class a, Class b, Class c);

如何做到这一点?

除了以字符串形式按名称传递方法组(一组名称相同但签名不同的方法)外,没有其他方法可以传递方法组,然后使用反射。任何
委托
/
表达式
/
方法信息
表示都必须指向单个方法

所以:你不能


建议:

interface IBar {
    void bar(Class a, Class b, Class c);
    void bar(Class a, Class b, Class c, Class d);
}
class YourType : IBar {
    void IBar.bar(Class a, Class b, Class c) {...}
    void IBar.bar(Class a, Class b, Class c, Class d) {...}
    ...
    public main()
    {
        foo(this);
    }
}

您可以对两个
bar
函数进行不同的命名,使用两个重载的
foo
函数,并将条件从
foo
移动到
main

    protected void bar1(int a, int b, int c)
    {
        Console.WriteLine("a={0}, b={1}, c={2}", a, b, c);
    }

    protected void bar2(int a, int b, int c, int d)
    {
        Console.WriteLine("a={0}, b={1}, c={2}, d={3}", a, b, c, d);
    }

    public void foo(Action<int, int, int> bar)
    {
        int a = 1;
        int b = 2;
        int c = 3;
        bar1(a, b, c);
    }

    public void foo(Action<int, int, int, int> bar)
    {
        int a = 1;
        int b = 2;
        int c = 3;
        int d = 4;
        bar2(a, b, c, d);
    }

    public void main()
    {
        if (condition)
            foo(bar1);
        else
            foo(bar2);
    }
受保护的空条1(内部a、内部b、内部c)
{
WriteLine(“a={0},b={1},c={2}”,a,b,c);
}
受保护的空条2(内部a、内部b、内部c、内部d)
{
WriteLine(“a={0},b={1},c={2},d={3}”,a,b,c,d);
}
公共无效foo(操作栏)
{
INTA=1;
int b=2;
int c=3;
bar1(a,b,c);
}
公共无效foo(操作栏)
{
INTA=1;
int b=2;
int c=3;
int d=4;
bar2(a,b,c,d);
}
公共图书馆
{
如果(条件)
foo(bar1);
其他的
foo(bar2);
}

最后两行中是否有一行涉及第四个参数?另外:我怀疑您要查找的单词是重载的,而不是重写的。不同的概念。我还有其他方法吗?@用户可能有一个方法,一个
操作
,并将一个
null
作为第四个参数传递…?是的,我以前想过。但这个解决方案和这个问题一样。我现在的情况更复杂。因此,我认为这对我来说是不可能的。@user1510539在这种情况下,最好的办法是使用所需的两种方法声明一个
接口,并将参数更改为接口。委托基本上是一种声明单个方法接口的廉价方式;但是如果你有多种方法:你需要一个合适的接口;看我的编辑嗯,我看。。。但是“foo”的语法是什么样的呢?
    protected void bar1(int a, int b, int c)
    {
        Console.WriteLine("a={0}, b={1}, c={2}", a, b, c);
    }

    protected void bar2(int a, int b, int c, int d)
    {
        Console.WriteLine("a={0}, b={1}, c={2}, d={3}", a, b, c, d);
    }

    public void foo(Action<int, int, int> bar)
    {
        int a = 1;
        int b = 2;
        int c = 3;
        bar1(a, b, c);
    }

    public void foo(Action<int, int, int, int> bar)
    {
        int a = 1;
        int b = 2;
        int c = 3;
        int d = 4;
        bar2(a, b, c, d);
    }

    public void main()
    {
        if (condition)
            foo(bar1);
        else
            foo(bar2);
    }