如何将外部程序集的类型添加到工具箱控件? (WPF)

发布于 2024-10-09 23:55:12 字数 710 浏览 2 评论 0原文

我试图在我的 WPF 应用程序中执行类似的操作:

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

即为程序集中找到的每种类型添加 ToolboxItemWrappers。然而,最后一行抛出以下异常(参见图片)

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

外部程序集的所有依赖项也在主(WPF)应用程序中引用。那么这里出了什么问题以及如何修复它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

踏月而来 2024-10-16 23:55:12

您正在使用 Assembly.LoadFile 通过反射加载程序集。但是此方法不会自动在同一目录中查找依赖项。您应该使用Assembly.LoadFrom

另请注意,LoadFrom 通过 Fusion,允许将加载请求重定向到另一个程序集,而 LoadFile 则准确加载您请求的内容。

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.

潜移默化 2024-10-16 23:55:12

文件中是否有 CustomLibrary 程序集?如果没有,请在应用程序中挂钩此事件 AppDomain.CurrentDomain.AssemblyResolve,并加载 filePath 处的程序集引用的任何其他程序集。如果 CustomLibrary 或其他 dll 不在 GAC 中,则需要它。

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.

通知家属抬走 2024-10-16 23:55:12

确保 GAC 中安装了“CustomLisbrary”。此外,您可能想要创建 design.dll 和 VisualStudio.design.dll。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文