【发布时间】:2016-06-09 12:56:51
【问题描述】:
我有一个简单的问题可能很容易回答,但大量使用 google 并没有为我的问题找到答案。因此,如果有正确的解决方案,我深表歉意,但我没有看到它。
如果我有类似的方法调用
Object.Add(string text, System.Drawing.Color color);
那是给某个对象添加一些文本,指定颜色,我想动态地改变颜色,然后我可以输入一些东西。喜欢
Object.Add("I'm a string", SomeBool ? Color.Red : Color.Green);
这很有帮助,但一旦我想比较两个以上的情况,它就会失败。
我正在寻找类似(伪代码)的东西
Object.Add("I'm another string", new delegate (Sytem.Drawing.Color)
{
if (tristate == state.state1)
{
return Color.Blue;
}
else if (tristate == state2)
{
return Color.Green;
}
// ...
});
但无论我尝试什么,它都会引发编译器错误。
我尝试了很多关于如何将函数作为方法参数传递的谷歌,但我发现很像
public void SomeFunction(Func<string, int> somefunction)
{
//...
}
这不是我的问题。
谢谢你:)
【问题讨论】:
标签: c# function methods delegates invoke