我想创建一个树视图,列出程序集中的所有属性。我可以使用以下代码生成根节点:
mainAssembly = Assembly.LoadFile(filename); //Global Variable
Type[] objTypes = mainAssembly.GetTypes().OrderBy(o=>o.Name).ToArray();
foreach (var type in objTypes)
{
TreeViewItem item = new TreeViewItem();
item.Header = type.Name;
item.Foreground = Brushes.White;
item.ToolTip = type.FullName;
tvEntities.Items.Add(item);
}
单击根节点 [类名] 时,我想列出该特定类中包含的属性。但是,如果它包含类型为class1的聚合属性,它位于另一个程序集中,它会给我IOFileNotFound Exception 错误。
private void ItemExpanded(object sender, RoutedEventArgs e)
{
try
{
TreeViewItem item = e.OriginalSource as TreeViewItem;
if (item.ToolTip != null)
{
Type assemblyType = mainAssembly.GetType(item.ToolTip.ToString());
if (assemblyType != null)
{
foreach (var prop in assemblyType.GetProperties())
{
PropertyInfo property = prop;
TreeViewItem childItem = new TreeViewItem();
childItem.Header = property.Name;
/*Following line gives IOFileNotFound exception, if property is declared in some other assembly.*/
childItem.ToolTip = property.PropertyType.FullName;
item.Items.Add(childItem);
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
如何加载这些引用的程序集并显示树状结构。