用反射和Dictionary替代if else

来源:互联网 发布:淘宝店铺海报怎么做 编辑:程序博客网 时间:2024/05/22 14:50

原始代码

<pre name="code" class="csharp">public static int startprocess(string SCHEDULERID)        {if (SCHEDULERID.ToUpper() == "IVDBUPLOAD")                {                                       Dosth();                }                else if (SCHEDULERID.ToUpper() == "OMASIA")                {                   Dosth();                }                else if (SCHEDULERID.ToUpper() == "IVDBPATCH")                {                    Dosth();                }                else if (SCHEDULERID.ToUpper() == "ASIAPATCH")                { Dosth();                }                else if (SCHEDULERID.ToUpper() == "OMEUR")                {                    Dosth();                }                else if (SCHEDULERID.ToUpper() == "EURPATCH")                { Dosth(); }}


现在改为

<pre name="code" class="csharp">public class Program    {public delegate void DoSomething();public static int startprocess(string SCHEDULERID)        {          InvokeFun(SCHEDULERID);                  }public static void InvokeFun(string SCHEDULERID)        {            SCHEDULERID = SCHEDULERID.ToUpper();            Dictionary<string, DoSomething> d = new Dictionary<string, DoSomething>()            {                {"IVDBUPLOAD", new DoSomething(IVDBUPLOADObj)},                {"OMASIA", new DoSomething(OMASIAObj)},                {"OMEUR", new DoSomething(OMASIAObj)},                {"ASIAPATCH",new DoSomething(ASIAPATCHObj)},                {"IVDBPATCH",new DoSomething(IVDBPatchObj)},                {"IVDBPATCH",new DoSomething(IVDBPatchObj)}            };            d[SCHEDULERID].Invoke();        }        public static void IVDBUPLOADObj()        {                    }        public static void OMASIAObj()        {                    }        public static void IVDBPatchObj()        {        }        public static void ASIAPATCHObj()        {                    }        public static  void OMEURObj()        {                    }}


0 0
原创粉丝点击