简体   繁体   中英

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.

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. Can anyone suggest an alternative way of using switch statement.

Based on the comments under your question, I discovered it is possible to implement what @Jon Skeet stated.

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 ):

 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:

 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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