C# 委托给具有不同可选参数的方法
我知道我可以声明具有默认常量参数的委托,例如:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我理解正确的话,您的
myFunc
将完全省略val
参数,始终使用您最终调用的方法的默认值,对吗?如果是这样,这应该可以实现您正在寻找的内容:
If I understand you correctly, your
myFunc
will leave out theval
parameter entirely, always using the default for whichever method you're ultimately calling, correct?If so, this should accomplish what you're looking for:
使用 lambda 绑定所需的值。默认值实际上是重载,它将您想要的值绑定到函数参数。由于您有各种 if 来分隔函数,因此只需将委托传递给仅接受整数的函数即可。
换句话说,
等等..
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,
and so on..
您要求委托有一个可选参数,该参数根据它调用的方法而变化,这是无法实现的。可选参数是通过在编译时重写方法的调用点来实现的。如果委托指向的方法是在运行时确定的,那么它在编译时会写什么? 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.