如何将外部程序集的类型添加到工具箱控件? (WPF)
我试图在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在使用
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 useAssembly.LoadFrom
.Also take in consideration that
LoadFrom
goes through Fusion allowing the load request to be redirected to another assembly whileLoadFile
loads exactly what you requested.文件中是否有 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.
确保 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.