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

时间:2021-05-11 10:47:57

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

效果:

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 > )
{
ClassList = new List<string>();
var Companies = e.AddedItems[].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 > )
{
var Companies = e.AddedItems[].ToString();
RightModule.ClassName = Companies;
}
e.Handled = true;
}
catch (Exception ex)
{ throw ex;
}
}

wpf窗体:

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