gpt4 book ai didi

c# - 如何在树状结构中列出程序集(包括引用的程序集)中的所有属性?

转载 作者:太空宇宙 更新时间:2023-11-03 16:32:34 25 4
gpt4 key购买 nike

我想创建一个 TreeView ,列出程序集中的所有属性。我能够使用以下代码生成根节点:

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);
}

单击根节点 [class name],我想列出该特定类中包含的属性。但是,如果它包含类型为 class1 的聚合属性,它位于另一个程序集中,它会给我 IOFileNotFound 异常错误。

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;
}
}

如何加载这些引用的程序集并显示树状结构。

最佳答案

确保所有引用的程序集都复制到应用程序的文件夹中。您获得异常的原因是 CLR 找不到引用的程序集之一。

关于c# - 如何在树状结构中列出程序集(包括引用的程序集)中的所有属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10382161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com