没有参数的方法如何分配给 ExpandoObject?
我正在尝试使用以下签名将方法(函数)分配给 ExpandoObject:
public List<string> CreateList(string input1, out bool processingStatus)
{
//method code...
}
我尝试执行类似以下代码的操作,但该代码无法编译:
dynamic runtimeListMaker = new ExpandoObject();
runtimeListMaker.CreateList =
new Func<string, bool, List<string>>(
(input1, out processingStatus) =>
{
var newList = new List<string>();
//processing code...
processingStatus = true;
return newList;
});
不幸的是,我无法更改 CreateList 签名,因为它会破坏向后兼容性,因此需要重写这不是一个选择。我尝试通过使用委托来解决此问题,但在运行时,我收到“无法调用非委托类型”异常。我想这意味着我没有正确分配委托。我需要帮助使语法正确(委托示例也可以)。谢谢!!
I'm trying to assign a method (function) to an ExpandoObject with this signature:
public List<string> CreateList(string input1, out bool processingStatus)
{
//method code...
}
I've tried to do something like this code below which doesn't compile:
dynamic runtimeListMaker = new ExpandoObject();
runtimeListMaker.CreateList =
new Func<string, bool, List<string>>(
(input1, out processingStatus) =>
{
var newList = new List<string>();
//processing code...
processingStatus = true;
return newList;
});
Unfortunately I cannot change the CreateList signature because it will break backward compatibility so rewriting it is not an option. I've tried to get around this by using delegates but at runtime, I got an "Cannot invoke a non-delegate type" exception. I guess this means I'm not assigning the delegate correctly. I need help getting the syntax correct (delegate examples are OK too). Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该示例按预期编译并运行:
您的语句的问题是您的 Func 没有像您的签名那样使用输出参数。
如果你想分配当前的方法,你将无法使用 Func,但你可以创建你自己的委托:
This sample compiles and runs as expected:
The problem with your statement is that your Func does not use an output parameter like your signature does.
If you want to assign your current method, you won't be able to use Func, but you CAN create your own delegate:
代码中使用
out
参数类型作为processingStatus
参数的问题 在 C# 中不允许。The problem in your code that using
out
parameter type forprocessingStatus
parameter which is not allowed in C#.