C# 表达式树不能包含使用可选参数的调用或调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12413583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
An expression tree may not contain a call or invocation that uses optional arguments
提问by bevacqua
An expression tree may not contain a call or invocation that uses optional arguments
表达式树不能包含使用可选参数的调用或调用
return this.RedirectToAction<MerchantController>(x => x.Edit(merchantId));
Where edit had a second, nullable argument.
其中 edit 有第二个可为空的参数。
Why is this?
为什么是这样?
采纳答案by usr
The underlying expression tree APIdoes not support optional arguments.
该基础表达式树API不支持可选参数。
For IL-compiled code the C# compiler inserts the default values at compile time (hard-coded), because the CLR does not support calling methods with optional arguments either when the arguments are not provided explicitly.
对于 IL 编译的代码,C# 编译器在编译时插入默认值(硬编码),因为 CLR 不支持在未显式提供参数时调用带有可选参数的方法。
回答by ds4940
Had the same message when trying to use Mock.setup to mock a method with multiple default parameters. I just had to add the additional parameters in the lambda.
尝试使用 Mock.setup 模拟具有多个默认参数的方法时出现相同的消息。我只需要在 lambda 中添加额外的参数。
void someMethod(string arg1 = "", string arg2 = "")
mockedObject.Setup(x => x.someMethod(It.IsAny<string>(), It.IsAny<string>()))

