简体   繁体   English

在同一类中的多个方法中的Assembly.Load()可以加载同一程序集-是否最佳?

[英]Assembly.Load() in multiple methods within the same class to load the same assembly - optimal or not?

I have a class with several methods (eg Method1, Method2, Method3) and inside each method I load an assembly (same one in each method) and invoke a certain method (reflection) like this: 我有一个带有多个方法的类(例如Method1,Method2,Method3),并且在每个方法内部我都加载一个程序集(每个方法中的一个)并像下面这样调用某个方法(反射):

void Method1() // error handling etc. omitted
{
    Assembly asm = Assembly.LoadFrom(path);

    Type type = asm.GetType(classname);
    MethodInfo methodInfo = type.GetMethod("MethodName", ...);
    var o = Activator.CreateInstance(type); 
    var result = methodInfo.Invoke(o, parameters);
}

Is it OK performance-wise to do reflection this way, provided that all these methods load the same assembly? 如果所有这些方法都加载相同的程序集,以这种方式执行反射是否可以实现性能? The code then looks like this: 代码如下所示:

obj.Method1(); // calls Assembly.Load()
obj.Method2(); // calls Assembly.Load()
obj.Method3(); // calls Assembly.Load()

or would it be better to load the assembly (and possibly GetType) only once in a separate method, store the reference to a class Assembly property and inside methods (Method1, Method2, Method3..) use this reference instead of always calling Assembly.Load()? 还是在一个单独的方法中仅加载一次程序集(可能是GetType),将对类Assembly属性的引用存储起来,然后在内部方法(Method1,Method2,Method3 ..)中使用此引用,而不是始终调用Assembly会更好。加载()? This way, the code would look something like: 这样,代码将类似于:

obj.LoadMyAssembly(); // calls Assembly.Load() and uses obj.MyAssembly property to store the reference
obj.Method1() // doesn't call Assembly.Load(), uses obj.MyAssembly instead
obj.Method2() // doesn't call Assembly.Load(), uses obj.MyAssembly instead
obj.Method3() // doesn't call Assembly.Load(), uses obj.MyAssembly instead

Does it make any difference? 有什么区别吗? Domains etc. are never changed (or even used) inside the code. 域等永远不会在代码内部更改(甚至使用)。

There is no big difference as the assembly will be loaded in the same context. 没有什么大的区别,因为程序集将在相同的上下文中加载。 Then, if the assembly is already loaded, it won't reload it even if you call Assembly.LoadFrom(path); 然后,如果程序集已经加载,即使调用Assembly.LoadFrom(path);也不会重新加载该程序集Assembly.LoadFrom(path);

However, if you already know that all the method are in the same assembly, it could be better to call Assembly.LoadFrom only once. 但是,如果您已经知道所有方法都在同一个程序集中,则最好只调用一次Assembly.LoadFrom

As far as I know, load the same assembly more than once would not cause performance issues. 据我所知,多次加载同一程序集不会导致性能问题。 However, look into your method, there is 但是,看看您的方法,有

var o = Activator.CreateInstance(type); 

You are invoking a member method, on a definitely a new instance of object of the type. 您正在绝对类型的object的新实例上调用成员方法。

Though it might not cause problems, but just a redundant behavior, isn't it? 虽然这可能不会引起问题,但只是多余的行为,不是吗? I think that just wasting, and increase garbages to collect on your heap. 我认为这只是浪费,并增加了垃圾收集量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM