简体   繁体   English

C#代表具有不同可选参数的方法

[英]C# Delegates to methods with different optional parameters

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. 我希望能够无参数调用myFunc或使用skip参数调用myFunc In this part of code, the first parameter, val , is NOT used. 在这部分代码中,不使用第一个参数val (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. 我想保留该方法的默认val参数,无论它是什么。

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? 如果我理解正确,你的myFunc将完全省略val参数,总是使用你最终调用的方法的默认值,对吗?

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 ...
    ...
}

Use a lambda to bind the value that you want. 使用lambda绑定所需的值。 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. 由于你有各种各样的if分离出函数,只需将委托传递给只接受整数的函数。

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.. 等等..

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. Eric Lippert关于可选参数的系列非常具有启发性。

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. 使用MethodInfo.GetParameters()获取参数info,使用ParameterInfo.DefaultValue获取默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM