-1

I have a class with several methods (e.g. Method1, Method2, Method3) and inside each method I load an assembly (same one in each method) and invoke a certain method (reflection) like this:

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()? 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.

w128
  • 4,680
  • 7
  • 42
  • 65
  • 2
    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);_ – Cédric Bignon Jan 30 '13 at 15:16
  • Thanks. Feel free to write this as an answer so that I can accept it. (note, with Assembly.Load() I actually mean Assembly.LoadFrom(), but that shouldn't really make a difference). – w128 Jan 30 '13 at 15:16
  • IF your concern is performance THEN use a profiler... IMHO it is rather bad code duplication and will later on cause several problems in maintainance/debugging etc. - so I would definitely change it independently of performance considerations – Yahia Jan 30 '13 at 15:18
  • Assembly.LoadFrom(path) is actually obsolete. does Assembly.Load() not do what you need it to? You can load the Assembly into memory with File.ReadAllBytes() and call Assembly.Load() on that if you need to load from a file. Don't quote me on this, but I think this prevents a lot of weird bugs that come with LoadFrom(). – Gray Jan 30 '13 at 15:19
  • What kind of maintainance/debugging problems may this cause? What I'm doing is not really performance demanding, but I would still prefer to write clean and optimal code. – w128 Jan 30 '13 at 15:21
  • I don't want to speak on topics I do not know, maybe someone else can help with why it is obsolete? I just avoid using stuff marked obsolete because I am a sheep (maybe a smart sheep?). I have heard stuff about locking the dll and screwing up builds, but that may not even apply to you. [This article](http://msdn.microsoft.com/en-us/library/dd153782.aspx) from microsoft might give you some explanation and general info on best practices, but I didn't really see anything about why LoadFrom(string path) is obsolete now. – Gray Jan 30 '13 at 15:38
  • Maybe I'm missing something, but MSDN only lists LoadFrom(String, Evidence) as obsolete, but not LoadFrom(String)? [link](http://msdn.microsoft.com/en-us/library/system.reflection.assembly.loadfrom.aspx) – w128 Jan 30 '13 at 15:44

2 Answers2

1

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

However, if you already know that all the method are in the same assembly, it could be better to call Assembly.LoadFrom only once.

Cédric Bignon
  • 12,892
  • 3
  • 39
  • 51
1

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.

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.

Ken Kin
  • 4,503
  • 3
  • 38
  • 76