DllImport 而不是缓慢的 Assembly.LoadFrom

发布于 2024-08-23 01:43:51 字数 653 浏览 1 评论 0原文

我有几个资源 DLL,当前在应用程序启动时使用以下代码加载:

Assembly ass = Assembly.LoadFrom(fi.FullName);
Type t = ass.GetTypes()[0];
string ns = t.Namespace;
BaseFacade bf = Activator.CreateInstance(t) as BaseFacade;

// bf.GoWild()...

当我拥有 BaseFacade 时,我会疯狂地调用函数以从 DLL 获取资源,所有这些都工作正常。然而,最初的 LoadFrom 非常慢,对于 10 个 DLL,我需要花费 30 秒以上。

所以,我想知道其他方法?有吗?我想知道是否可以做类似的事情:

[DllImport("myResources1.dll")]
public static extern void GoWild();

[DllImport("myResources2.dll")]
public static extern void GoWild();

??如果可能的话,我将如何为资源 DLL 公开这些 GoWild 函数?另外,考虑到 DLL 并不总是位于主 DLL 的目录中(并且用户可以选择移动这些 DLL),我如何将应用程序指向 DLL 的位置?

感谢您对此主题的任何建议!

I have couple resource DLLs that I currently load when application starts using following code:

Assembly ass = Assembly.LoadFrom(fi.FullName);
Type t = ass.GetTypes()[0];
string ns = t.Namespace;
BaseFacade bf = Activator.CreateInstance(t) as BaseFacade;

// bf.GoWild()...

When I have that BaseFacade I go wild with function calling to obtain resources from DLLs and all that works fine. However that initial LoadFrom is extremely slow and for 10 DLLs I have it takes over 30 seconds.

So, I was wondering of alternative approaches? Are there any? I was wondering if it is possible to do something like:

[DllImport("myResources1.dll")]
public static extern void GoWild();

[DllImport("myResources2.dll")]
public static extern void GoWild();

?? If it is possible, how would I expose those GoWild functions for resource DLLs? Also, how would I point application to location of DLLs considering they are not always in directory of main DLL (and user has option to move those DLLs around)?

Thanks for any advice on this subject!

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

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

发布评论

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

评论(3

晨与橙与城 2024-08-30 01:43:51

可能提高性能的一件事是不从加载的程序集中检索所有类型。如果它们那么大(10 MB),我假设每个反射都需要处理大量类型。换句话说,摆脱 ass.GetTypes() 调用,因为您显然只需要程序集中的一种类型。

顺便说一句,访问返回类型数组中的第一个元素似乎有风险,因为反射不能保证返回类型的顺序。

您可以定义一个程序集级自定义属性来指定应加载从 BaseFacade 派生的类型。

One thing that might improve perf is to not retrieve all types from the loaded assembly. If they are that big (10 MB) I assume there are a significant number of types in each that Reflection needs to work its way through. In other words, get rid of the ass.GetTypes() call since you apparently only need one single type from the assembly.

BTW, accessing the first element in the returned type array seems risky since Reflection doesn't guarantee the order in which the types are returned.

You could instead define an assembly-level custom attribute that specifies which type, derived from BaseFacade, that should be loaded.

狼性发作 2024-08-30 01:43:51

LoadFrom 的缓慢是由于 DLL 中发生的初始化造成的,还是 .NET 框架中的某些问题?

这可能会有所帮助:http://dedjo。 blogspot.com/2008/01/how-to-load-unmanagement-native-resources.html

Is the slowness of LoadFrom due to initialization happening in the DLLs, or is it something in the .NET framework?

This may help: http://dedjo.blogspot.com/2008/01/how-to-load-unmanaged-native-resources.html

终陌 2024-08-30 01:43:51

从您的第一个代码片段来看,您正在通过反射加载 .NET 库。 DllImport 用于加载非托管库,而不是.NET 程序集。

如果对应用程序的其余部分没有太多了解,就很难对您已经在做的事情提出任何替代方法。您能提供后台线程中的第一个代码片段吗?

It looks from your first snippet that you are loading .NET libraries through reflection. DllImport is for loading unmanaged libraries, not .NET assemblies.

Without much knowledge of the rest of the application, it's hard to suggest any alternative method to what you are doing already. Could you the first code snippet in a background thread?

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