mvvm模式下在WPF项目中动态加载项目的程序集和类

2017-06-29 11:25:59来源:cnblogs.com作者:Jugg书生人点击

在mvvm模式的wpf项目中有个需求需要去加载解决方案的程序集,并且根据程序集去动态加载当前程序集的类,做成下拉框形式。

效果:

//全局定义 private ComboBox abList= null;        /// <summary>        /// 加载所有项目的程序集        /// </summary>        public void LoadAssembly()        {            abList=new ComboBox();            IList<FileInfo> list = new List<FileInfo>();            FileInfo[] files = new DirectoryInfo(System.Environment.CurrentDirectory).GetFiles("*.dll");            foreach (FileInfo info2 in files)            {                try                {                    if (info2.Name.Contains("Reform"))                    {                        abList.Items.Add(info2);                    }                 }                catch                {                    continue;                }                            }
        }
//属性定义 private List<string> assemblyList;        /// <summary>        /// 程序集列表下拉框数据源        /// </summary>        public List<string> AssemblyList        {            get { return assemblyList; }            set            {                assemblyList = value;                NotifyOfPropertyChange("AssemblyList");            }        } private List<string> classList;        /// <summary>        ///类列表下拉框数据源        /// </summary>        public List<string> ClassList        {            get { return classList; }            set            {                classList = value;                NotifyOfPropertyChange("ClassList");            }        }
//初始化数据public InitData (){//给程序集下拉框赋值foreach (var assemblyListItem in abList.Items){AssemblyList.Add(assemblyListItem.ToString());}.......}
/// <summary>        /// 程序集改变事件         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>         public void abList_SelectionChanged(object sender, SelectionChangedEventArgs e)        {            try            {                if (e != null && e.AddedItems.Count > 0)                {                    ClassList = new List<string>();                    var Companies = e.AddedItems[0].ToString();                    //加载对应类                    foreach (var assemblyItem in abList.Items)                    {                        if (assemblyItem.ToString() == Companies)                        {                            FileInfo selectedItem = assemblyItem as FileInfo;                            if (selectedItem != null)                            {                                System.Type[] types = Assembly.LoadFile(selectedItem.FullName).GetTypes();                                foreach (System.Type type2 in types)                                {                                    if ((type2.IsClass && type2.IsPublic)&& type2.FullName.Contains("Reform"))                                    {                                        ClassList.Add(type2.FullName);//类下拉框赋值                                    }                                }                            }                            break;                        }                    }                }                e.Handled = true;            }            catch (Exception ex)            {                throw ex;            }        }        /// <summary>        /// 类选择事件        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        public void classList_SelectionChanged(object sender, SelectionChangedEventArgs e)        {            try            {                if (e != null && e.AddedItems.Count > 0)                {                    var Companies = e.AddedItems[0].ToString();                    RightModule.ClassName = Companies;                }                e.Handled = true;            }            catch (Exception ex)            {                throw ex;            }        }

wpf窗体:

//程序集下拉框                <ComboBox Grid.Row="5" Grid.Column="1" ItemsSource="{Binding AssemblyList}" SelectedIndex="0" cm:Message.Attach="[Event SelectionChanged]=[abList_SelectionChanged($source,$eventArgs)];"  Text="{Binding RightModule.AssemblyName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left"  Width="252" Height="25"  IsEditable="True" Margin="5,3"></ComboBox>//类下拉框                <ComboBox Grid.Row="6" Grid.Column="1" ItemsSource="{Binding ClassList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SelectedIndex="0" cm:Message.Attach="[Event SelectionChanged]=[classList_SelectionChanged($source,$eventArgs)];"  Text="{Binding RightModule.ClassName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left"  Width="252" Height="25"  IsEditable="True" Margin="5,3"></ComboBox>

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台