How to add types from external assembly to toolbox control? (WPF)

你说的曾经没有我的故事 提交于 2019-12-10 23:58:12

问题


I am trying to do something like this in my WPF application:

    ToolboxControl ctrl = new ToolboxControl();
    Assembly assembly = Assembly.LoadFile(file);
    var category = new ToolboxCategory(assembly.GetName().Name);
    foreach (Type t in assembly.GetTypes())
    {
        var wrapper = new ToolboxItemWrapper(t, t.Name);
        category.Add(wrapper);
    }
    ctrl.Categories.Add(category);

i.e. adding ToolboxItemWrappers for each type found in an assembly. However the last line throws the following exception (see image)

http://img41.imageshack.us/img41/2261/7xvqv.png http://img41.imageshack.us/img41/2261/7xvqv.png

All dependencies of the external assembly are also referenced in the main (WPF) application. So what's wrong here and how to fix it?


回答1:


Is the CustomLibrary assembly in the file? If not, hook to this event AppDomain.CurrentDomain.AssemblyResolve in you app, and load any other assemblies that the assembly at the filePath references to. It is required if CustomLibrary or other dlls are not in GAC.




回答2:


You are using Assembly.LoadFile to load the assembly through reflection. However this method does not automatically find dependencies in the same directory. You should use Assembly.LoadFrom.

Also take in consideration that LoadFrom goes through Fusion allowing the load request to be redirected to another assembly while LoadFile loads exactly what you requested.




回答3:


Make sure "CustomLisbrary" is installed in GAC. Additionally, You may want to create design.dll and VisualStudio.design.dll.



来源:https://stackoverflow.com/questions/4594968/how-to-add-types-from-external-assembly-to-toolbox-control-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!