C# 简化优化if/switch 表驱动法

表示这个很强大

字典加反射,搞定多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(); } } } }

 

posted @ 2014-08-22 16:40  iEvent  阅读(2398)  评论(0编辑  收藏  举报