【问题标题】:Function as method call parameter作为方法调用参数的函数
【发布时间】: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


    【解决方案1】:

    把你的逻辑放在第一位:

    Color color;
    
    if (tristate == state1) 
        color = Color.Blue;
    else if (tristate == state2)
        color = Color.Green;
    else
        color = Color.Red;
    
    Object.Add("I'm a string", color);
    

    您的delegate 解决方案不起作用的原因仅仅是new delegate (Sytem.Drawing.Color) { … } 返回一个函数委托,在您获得颜色值之前需要先调用该委托。而且由于您的方法需要颜色而不是返回颜色的方法,因此它并没有真正的帮助。

    根据你的逻辑有多短,你仍然可以在这里使用三元条件运算符并简单地链接它:

    Object.Add("I'm a string", tristate == state1 ? Color.Blue : tristate == state2 ? Color.Green : Color.Red);
    

    这相当于上面的详细if/else if/else 结构。但当然,它不一定更具可读性,因此请谨慎使用并选择更具可读性的解决方案。

    【讨论】:

    • 哦,这符合我在某处读到的关于代表的内容。从逻辑上讲,委托不起作用。
    【解决方案2】:

    我建议使用字典,例如

      private static Dictionary<State, Color> s_Colors = new Dictionary<State, Color>() {
        {State1, Color.Blue},
        {State2, Color.Green},
        {State3, Color.Red},
      };
    
    
      ... 
    
      Object.Add("I'm a string", s_Colors[tristate]);
    

    【讨论】:

    • 相当不错的解决方案。喜欢它:)
    【解决方案3】:

    这将让您传递一个函数来决定状态并将颜色传递给一个动作,然后您可以决定要做什么。实际上,文本和颜色只能在 Add 方法中使用,根本不需要返回以供使用,但这只是您正在寻找的示例。因为您没有使用 Add 方法中的文本(在您的示例中以任何方式),所以我将其取出,它可以在操作中使用,否则只需将其添加回来并在 Add 方法中使用它。

    void Main()
    {
        Object.Add(() => SomeState.State2, (col) =>
        {
            Label1.Text = "Your text";
            //Do something with color
            Label1.BackColor = col;
        });
    
        //example 2
        Object.Add(() => 
           {
              return someBool ? SomeState.State1 : SomeState.State2;
           }, 
           (col) =>
           {
               Label1.Text = "Your text";
               //Do something with color
               Label1.BackColor = col;
           });
    }
    
    public static class Object
    {
        public static void Add(Func<SomeState> func, Action<Color> action)
        {
            switch(func())
            {
                case SomeState.State1: 
                    action(Color.Blue);
                    break;
                case SomeState.State2: 
                    action(Color.Green);
                    break;
                default: 
                    action(Color.Black);
                    break;
            }
        }
    }
    
    public enum SomeState
    {
        State1,
        State2
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-11
      • 2016-11-18
      • 2021-11-21
      • 1970-01-01
      • 1970-01-01
      • 2010-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多