C# 的委托方法可以采用隐式类型参数吗?

发布于 2024-09-26 06:31:32 字数 1268 浏览 0 评论 0原文

我很好奇是否可以使用隐式类型参数创建委托方法。这就是我想要实现的目标。这类事情不会为我编译,但它应该让您了解我想要完成的任务。

这是我想使用委托调用的函数:

class MyClass
{
    ArrayList function1(int param1, int param2)
    {
        ...do something...
    }
    ArrayList function2(string param3)
    {
        ...do something...
    }
}

这是调用它们的类:

class MyOtherClass
{
    delegate ArrayList delFunc(params object[] args);

    myOtherClass()
    {
        MyClass myClassInstance = new myClass();
        delFunc myDelFunc1 = myClassInstance.function1;
        delFunc myDelFunc2 = myClassInstance.function2;

        myDelFunc1(1,2);
        myDelFunc2("hello world");
    }


}

显然,在这个示例中,您实际上只是调用提供的函数。我实际上想做的是创建一个包装函数,它允许您提供一个函数作为具有任何可能的参数列表的委托。

我能想到的唯一解决方案是让 MyClass 中的函数也采用参数列表。然而,这会让任何直接调用这些函数的人感到非常困惑。我的意思是:

class MyClass
{
    ArrayList function1(params object[] args)
    {
        ...do something...
    }
    ArrayList function2(params object[] args)
    {
        ...do something...
    }
}

现在它可以编译,但我需要猜测或阅读注释,以找出要传递给 function1 和 function2 的内容。此外,曾经在编译时失败的地方,现在在运行时失败。所有非常糟糕的事情......

我想我可能缺少一些语法来做这样的事情

delegate ArrayList delFunc(MatchAnyArgumentSignature);

干杯, 抢

I'm curious if it's possible to create a delegate method with implicitly typed arguments. Here's what I'm trying to accomplish. This sort of thing won't compile for me, but it should give you an idea of what I'm trying to accomplish.

here's the functions I want to call using delegates:

class MyClass
{
    ArrayList function1(int param1, int param2)
    {
        ...do something...
    }
    ArrayList function2(string param3)
    {
        ...do something...
    }
}

And here's the class that calls them:

class MyOtherClass
{
    delegate ArrayList delFunc(params object[] args);

    myOtherClass()
    {
        MyClass myClassInstance = new myClass();
        delFunc myDelFunc1 = myClassInstance.function1;
        delFunc myDelFunc2 = myClassInstance.function2;

        myDelFunc1(1,2);
        myDelFunc2("hello world");
    }


}

Obviously in this example you would just actually call the provided functions. What I'm actually trying to do, is create a wrapper function which allows you to provide a function as a delegate with any possible argument list.

The only solution I can come up with is to make the functions in MyClass take param lists as well. However, that would make it very confusing to anyone directly calling these functions. Here's what I mean:

class MyClass
{
    ArrayList function1(params object[] args)
    {
        ...do something...
    }
    ArrayList function2(params object[] args)
    {
        ...do something...
    }
}

Now it compiles, but I need to guess, or read comments, to find out what to pass into function1 and function2. Also, where it once failed at compile time, it now fails at runtime. All very bad things...

I'm thinking there may be some syntax I'm missing for doing something like this

delegate ArrayList delFunc(MatchAnyArgumentSignature);

Cheers,
Rob

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦晓ヶ微光ヅ倾城 2024-10-03 06:31:33

只需使用 Action或动作:

public void Something(int arg1, int arg2, int arg3) {
  ...
}

以及方法内的任何位置:

Action<int, int, int>  call = Something; 
call(1, 2, 3);

在这种情况下不能使用 var。

不管怎样,你不应该再使用ArrayList了。

编辑:

如果这些委托应该返回某些内容,则有 Func、Func等。

Just use Action<T> or Action<T1, T2, .....> :

public void Something(int arg1, int arg2, int arg3) {
  ...
}

and anywhere inside a method:

Action<int, int, int>  call = Something; 
call(1, 2, 3);

You cannot use var in this case.

Anyways, you shouldn't use ArrayList anymore.

Edit:

If those delegates should return something, there is Func<TResult>, Func<T1..TN, TResult>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文