C# 委托给具有不同可选参数的方法

发布于 2024-11-30 12:29:54 字数 964 浏览 1 评论 0原文

我知道我可以声明具有默认常量参数的委托,例如:

delegate void MyDelegate(int x, int y = 5);

然后使用与签名匹配的任何方法在任何地方调用它。

好吧,我有很多这样声明的方法:

public Something FirstMethod(float val = 10, int skip = 0){ ... return sth; }
public Something SecondMethod(float val = 20, int skip = 0){ ... return sth; }
public Something ThirdMethod(float val = 5, int skip = 0){ ... return sth; }

...这个列表一直向上向下,无论如何,它们都有那个签名结构。这里的要点是,它们都有浮点参数,默认为不同的东西。

然后,我想创建一个指向以下方法之一的委托:

delegate Something ProblematicDelegateType(<<WHAT WILL GO HERE>>);
ProblematicDelegateType myFunc;
if(someValue == someParameter){
    myFunc = FirstMethod;
}else if(...){
    myFunc = SecondMethod;
}else...
...
}

myFunc();
myFunc(skip:100);

我希望能够无参数或使用 skip 参数调用 myFunc 。在这部分代码中,未使用第一个参数 val。 (它们在其他地方使用)。

什么将进入代表的参数列表?我想保留该方法的默认 val 参数,无论它是什么。

I know that I can declare delegates that have default constant parameters such as:

delegate void MyDelegate(int x, int y = 5);

and then call it anywhere with any method that matches the signature.

Ok, I've got LOTS of methods declared like this way:

public Something FirstMethod(float val = 10, int skip = 0){ ... return sth; }
public Something SecondMethod(float val = 20, int skip = 0){ ... return sth; }
public Something ThirdMethod(float val = 5, int skip = 0){ ... return sth; }

... this list goes up all the way down, anyway, they all have that signature structure. The point here is that, they all have floating point argument, that defaults to something different.

Then, I want to create a delegate that will point to one of these methods:

delegate Something ProblematicDelegateType(<<WHAT WILL GO HERE>>);
ProblematicDelegateType myFunc;
if(someValue == someParameter){
    myFunc = FirstMethod;
}else if(...){
    myFunc = SecondMethod;
}else...
...
}

myFunc();
myFunc(skip:100);

I want to be able to call myFunc both parameterless or with the skip parameter. In this part of code, the first parameter, val, is NOT used. (they are used elsewhere).

What will go to the delegate's argument list? I want to preserve that method's default val argument, whatever it is.

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

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

发布评论

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

评论(3

日裸衫吸 2024-12-07 12:29:54

如果我理解正确的话,您的 myFunc 将完全省略 val 参数,始终使用您最终调用的方法的默认值,对吗?

如果是这样,这应该可以实现您正在寻找的内容:

delegate Something ProblematicDelegateType(int skip = 0);
ProblematicDelegateType myFunc;
if (someValue == someParameter) {
    myFunc = skip => FirstMethod(skip: skip);
} else if (...) {
    myFunc = skip => SecondMethod(skip: skip);
} else ...
    ...
}

If I understand you correctly, your myFunc will leave out the val parameter entirely, always using the default for whichever method you're ultimately calling, correct?

If so, this should accomplish what you're looking for:

delegate Something ProblematicDelegateType(int skip = 0);
ProblematicDelegateType myFunc;
if (someValue == someParameter) {
    myFunc = skip => FirstMethod(skip: skip);
} else if (...) {
    myFunc = skip => SecondMethod(skip: skip);
} else ...
    ...
}
尽揽少女心 2024-12-07 12:29:54

使用 lambda 绑定所需的值。默认值实际上是重载,它将您想要的值绑定到函数参数。由于您有各种 if 来分隔函数,因此只需将委托传递给仅接受整数的函数即可。

换句话说,

delegate Something ProblematicDelegateType(int skip = 0)

//...

ProblematicDelegateType myDelegate;
if (someCondition)
  myDelegate = (_1) => FirstMethod(10, _1);
else if (someOtherCondition)
  myDelegate = (_1) => SecondMethod(20, _1);

等等..

Use a lambda to bind the value that you want. Default values are effectively overloads that have bound the value you desire to the function parameter. Since you have the various if's separating out the functions, just pass around a delegate to a function that only takes a integer.

In other words,

delegate Something ProblematicDelegateType(int skip = 0)

//...

ProblematicDelegateType myDelegate;
if (someCondition)
  myDelegate = (_1) => FirstMethod(10, _1);
else if (someOtherCondition)
  myDelegate = (_1) => SecondMethod(20, _1);

and so on..

薄荷→糖丶微凉 2024-12-07 12:29:54

您要求委托有一个可选参数,该参数根据它调用的方法而变化,这是无法实现的。可选参数是通过在编译时重写方法的调用点来实现的。如果委托指向的方法是在运行时确定的,那么它在编译时会写什么? Eric Lippert 关于 可选参数非常有启发性。

但是,您也可以考虑使用反射来获取默认值。使用 MethodInfo.GetParameters() 获取参数信息和 ParameterInfo.DefaultValue获取默认值。

Your requirement that the delegate have an optional parameter that varies depending on the method it invokes can't be done. Optional parameters are implemented by rewriting the call site of the method when it is compiled. If the method the delegate points to is determined at run-time what would it write at compile-time? Eric Lippert's series on optional arguments is very enlightening.

However, you could also look into using reflection to get the default value. Use MethodInfo.GetParameters() to get the parameter info and ParameterInfo.DefaultValue to get the default value.

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