C#簡略化if/switchテーブル駆動法の最適化

15518 ワード

これはとても強いです.
辞書は反射して、多windowのswitchを解決します
    public partial class MainWindow : Window

    {

        Dictionary<string, Type> WindowList = new Dictionary<string, Type>();

        public MainWindow()

        {

            InitializeComponent();

            this.AddHandler(Button.ClickEvent, new RoutedEventHandler(Btn_Click));

            LoadDic();

        }



        void LoadDic()

        {

            Type decoratorType = typeof(DecoratorWindow);

            Type viewboxType = typeof(ViewboxWindow);

            Type valueType = typeof(ValueWindow);

            Type lineType = typeof(LineWindow);

            Type winStartType = typeof(WinStartWindow);

            Type mp3Type = typeof(Mp3Window);

            Type socketType = typeof(SocketWindow);

            Type tagType = typeof(TagWindow);

            Type txtType = typeof(TXTWindow);

            Type QQType = typeof(QQWindow);

            Type transformType = typeof(TransformWindow);

            Type image3DWallType = typeof(Image3DWallWindow);

            Type transType = typeof(TransWindow);

            Type weatherType = typeof(WeatherWindow);

            Type datagridType = typeof(DatagridWindow);

            Type versionType = typeof(VersionWindow);

            WindowList.Add("Decorator", decoratorType);

            WindowList.Add("Viewbox", viewboxType);

            WindowList.Add("Value", valueType);

            WindowList.Add("Line", lineType);

            WindowList.Add("WinStart", winStartType);

            WindowList.Add("Mp3", mp3Type);

            WindowList.Add("Socket", socketType);

            WindowList.Add("Tag", tagType);

            WindowList.Add("TXT", txtType);

            WindowList.Add("QQ", QQType);

            WindowList.Add("Transform", transformType);

            WindowList.Add("Image3DWall", image3DWallType);

            WindowList.Add("Trans", transType);

            WindowList.Add("Weather", weatherType);

            WindowList.Add("Datagrid", datagridType);

            WindowList.Add("Version", versionType);

        }

        private void Btn_Click(object sender, RoutedEventArgs e)

        {

            Window window = (Window) Activator.CreateInstance(WindowList[(e.OriginalSource as Button).Tag.ToString()]);

            window.ShowDialog();

        }

    }

}
    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

            this.AddHandler(Button.ClickEvent,new RoutedEventHandler(Button_Click));

        }



        private void Button_Click(object sender, RoutedEventArgs e)

        {

            if ((e.OriginalSource as Button).Tag.ToString() == null) ;

            else

            {
          Window window = WindowList[(e.OriginalSource as Button).Tag.ToString()];  
switch ((e.OriginalSource as Button).Tag.ToString()) { case "Decorator": window = new DecoratorWindow(); break; case "Viewbox": window = new ViewboxWindow(); break; case "Value": window = new ValueWindow(); break; case "Line": window = new LineWindow(); break; case "WinStart": window = new WinStartWindow(); break; case "Mp3": window = new Mp3Window(); break; case "Socket": window = new SocketWindow(); break; case "Tag": window = new TagWindow(); break; case "TXT": window = new TXTWindow(); break; case "QQ": window = new QQWindow(); break; case "Transform": window = new TransformWindow(); break; case "Image3DWall": window = new Image3DWallWindow(); break; case "Trans": window Trans = new TransWindow(); break; case "Weather": window = new WeatherWindow(); break; case "Datagrid": window = new DatagridWindow(); break; default: break; }
          window.ShowDialog(); } } } }