如何解决Assembly.Load性能下降的问题?
我正在重构一些代码,但我陷入了困境。
假设我们有以下场景:
- 包含许多通用接口和类的核心程序集
- 包含更专业的类的库程序集。
库程序集引用核心程序集。到目前为止,一切都很好。
由于我正在重构它,因此核心程序集需要创建在库程序集中声明的对象。
好的,为了避免循环引用问题,我决定在需要时加载库程序集(并且仅在类型初始化时的非常特定的点上需要它)。
然而,整个事情的加载性能直线下降到黑暗的深渊......
有谁知道如何解决这个问题?
编辑添加
有些人请求我用来加载的代码...这真的很简单。
/*
* Load the Library Assembly
*/
Assembly asm = Assembly.Load("Library, PublicKeyToken=...");
/*
* Get desired type
*/
Type t = asm.GetType("Library.DesiredType")
/*
* Get the default constructor
*/
var ctor = type.GetConstructor(new Type[] {})
I'm refactoring some code and I've been hit with a dilemma.
Let's say we have the following scenario:
- A Core Assembly that contains many common interfaces and classes
- A Library Assembly that contains more specialized classes.
The Library Assembly references the Core Assembly. So far so good.
Due the fact that I'm refactoring this, there's a need for the Core Assembly to create objects that are declared in the Library Assembly.
OK, in order to avoid a circular reference problem, I decided to load the Library Assembly when needed (and it's needed only in a very specific point at the type initialization).
However, the loading performance of the whole thing plummeted to a dark abyss...
Does anyone know how to solve this?
Edited to add
Some people have requested the code I use to load... It's quite trivial, really.
/*
* Load the Library Assembly
*/
Assembly asm = Assembly.Load("Library, PublicKeyToken=...");
/*
* Get desired type
*/
Type t = asm.GetType("Library.DesiredType")
/*
* Get the default constructor
*/
var ctor = type.GetConstructor(new Type[] {})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该程序集只能加载到 AppDomain 中一次。重复调用加载程序集 X 应返回已加载的程序集。
您可以发布您如何尝试此操作的代码吗?您如何衡量“性能”?您是否对应用程序进行了分析以验证性能影响确实来自加载程序集?The assembly should only be loaded once into the AppDomain. Repeat calls to load assembly X should return the already-loaded assembly.
Can you post the code for how you're attempting this?How are you measuring "performance"? Have you profiled your application to verify the performance hit is indeed coming from loading the assembly?