I want to create a tree view which list down all properties in an assembly. I am able to generate the root nodes using the following code:
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);
}
On click of the root node [class name], I want to list down properties contained in that particular class. But if it contains an aggregated property of type class1, which is in another assembly, it gives me IOFileNotFound Exception error.
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;
}
}
How to load these referenced assemblies and show a tree like structure.
Make sure that all referenced assemlies are copied to your application’s folder. The reason you are getting the exception is that the CLR cannot find one of the referenced assemblies.