代码之家  ›  专栏  ›  技术社区  ›  Heydar

如何使接受参数或属性的方法与注释正则表达式中的方法类似

  •  -2
  • Heydar  · 技术社区  · 7 年前

    下面图片中的这种方法是什么?我如何制作这种方法?

    我在网上搜索了很多,什么也没找到。尝试了很多东西,比如属性方法或属性方法,但都没有发现。

    事实上,我的问题是我想让一个方法像这样工作,并且输入参数是可选的。

    My Questions Image 以下是我的代码(面板除外,其他面板为枚举类型):

    public static void Reset(System.Windows.Forms.Panel panel, formulaType formulaType, ShalvarType shalvarType = 0
                , DamanType damanType = 0, YaqeType yaqeType = 0, BalataneType balataneType = 0, AstinType astinType = 0)
            {
    
    
                object[,] collcetion = null;
                switch (formulaType)
                {
                    case formulaType.Shalvar:
                        collcetion = shalvarFurmula(shalvarType);
                        break;
                    case formulaType.Daman:
                        collcetion = damanFurmula(damanType);
                        break;
                    case formulaType.Yaqe:
                        collcetion = yaqeFurmula(yaqeType);
                        break;
                    case formulaType.Balatane:
                        collcetion = balataneFurmula(balataneType);
                        break;
                    case formulaType.Astin:
                        collcetion = astinFurmula(astinType);
                        break;
    
                }
    
                //System.Windows.Forms.TextBox
    
                for (int i = 0; i < collcetion.Length; i++)
                {
                    if (panel.Controls[collcetion[i, 0].ToString()].GetType().ToString() == "System.Windows.Forms.TextBox")
                    {
                        panel.Controls[collcetion[i, 0].ToString()].Text = collcetion[i, 1].ToString();
                    }
                    else
                    {
                        System.Windows.Forms.NumericUpDown num = panel.Controls[collcetion[i, 0].ToString()] as System.Windows.Forms.NumericUpDown;
                        num.Value = Convert.ToDecimal(collcetion[i, 1]);
                    }
                }
    
            }
    

    我想有面板和公式类型,但从第三个到最后都是这样。事实上,我给枚举类型的方式类似于图片。

    顺便说一下,我发送的代码还不完整。

    Thankx公司

    1 回复  |  直到 7 年前
        1
  •  1
  •   Camilo Terevinto Chase R Lewis    7 年前

    您的图像所指的是 Attribute 的可选属性,它们定义为:

    public class MyAttribute : Attribute
    {
        public string SomeData { get; set; }
    }
    

    您想要的是使用可选参数。您可以这样实现:

    public void DoSomething(string data = "", int age = 0) // data will be empty if no value is given
    {
    }
    

    然后可以使用以下两种方法调用此方法:

    DoSomething();
    DoSomething("some data");
    DoSomething(age: 10);