简体   繁体   English

C#中switch语句的替代方法

[英]Alternative for switch statement in c#

Can someone suggest an alternative way to solve this problem, I don't want to use SWITCH statement in my code. 有人可以建议解决此问题的另一种方法,我不想在代码中使用SWITCH语句。

Class Definition: 类定义:

public class Rootobject
{
    public Must[] must { get; set; }
    public Should[] should { get; set; }        
}  

public class Should 
{
    public Match match { get; set; }
    public Bool _bool { get; set; }
}

public class Must 
{        
    public Match match { get; set; }
    public Bool _bool { get; set; }
}

public class Match
{
    public string pname { get; set; }
}

public class Bool
{
    public string rname { get; set; }
}

Function Definition 功能定义

public root getobject(string op)
        {
            Rootobject root = new Rootobject();

            op ="must";

            switch (op)
            {
                case "should":
                    root.should = new Should[1];
                    Should objShould = new Should();
                    objShould.match = new Match();
                    objShould.match.pname = "hello";
                    root.should[0] = objShould;
                   break;
                case "must":
                    root.must = new Must[1];
                    Must objMust = new Must();
                    objMust.match = new Match();
                    objMust.match.pname = "hello";
                    root.must[0] = objMust;
                    break;
            }                       

       return(root);
        }

Switch statement is an overhead an new type comes then i may need to add another condition. Switch语句是一种新类型的开销,然后我可能需要添加另一个条件。 Can anyone suggest an alternative way of using switch statement. 任何人都可以建议使用switch语句的另一种方法。

Based on the comments under your question, I discovered it is possible to implement what @Jon Skeet stated. 根据您的问题下的评论,我发现可以实现@Jon Skeet所说的内容。

You can add an Initialize Method in your RootObject class to create the dictionary ( Use a ref Dictionary so to avoid setting the dictionary in your RootObject class that could change the structure of your serialization ): 您可以在RootObject类中添加一个Initialize方法来创建字典( 使用ref字典,以避免在RootObject类中设置字典,这可能会更改序列化的结构 ):

 public void Initialize(ref Dictionary<string, Func<Rootobject>> rootDic)
        {
            Func<Rootobject> shouldFunc = () =>
            {
                Rootobject root = new Rootobject();
                root.should = new Should[1];
                Should objShould = new Should();
                objShould.match = new Match();
                objShould.match.pname = "hello";
                root.should[0] = objShould;

                return root;
            };

            Func<Rootobject> mustFunc = () =>
            {
                Rootobject root = new Rootobject();
                root.must = new Must[1];
                Must objMust = new Must();
                objMust.match = new Match();
                objMust.match.pname = "hello";
                root.must[0] = objMust;

                return root;
            };
            rootDic.Add("should", shouldFunc);
            rootDic.Add("must", mustFunc);
        }

And then call it in your getobject method like so: 然后像这样在您的getobject方法中调用它:

 public static Rootobject getobject(string op)
        {
            Dictionary<string, Func<Rootobject>> rootDic = new Dictionary<string,Func<Rootobject>>();
            Rootobject root = new Rootobject();
            root.Initialize(ref rootDic);

            if(rootDic.Count > 0)
            return rootDic[op].Invoke();

            return new Rootobject();
        }

You still going to get the same result as the solution in your question even after serializing it. 即使将其序列化,您仍将获得与问题中的解决方案相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM