0

I want to load an assembly for the purposes of a plug-in functionality whereby the loaded dll may be reloaded without restarting the application. I understand the easiest way to do this is to load the assembly using:

 Assembly assembly = Assembly.Load(File.ReadAllBytes("myDerivedClass.dll"));

rather than:

Assembly assembly = Assembly.LoadFrom("myDerivedClass.dll");

However, when I used Assembly.Load instead of Assembly.LoadFrom, I get the error

object must implement iconvertible" when executing the following cast:

dynamic classObj = Convert.ChangeType(myBaseClassObject, type);

Any ideas what I am doing wrong / what is different between Load and LoadFrom to give this error?

Mahesh Waghmare
  • 726
  • 9
  • 27
  • where do myBaseClassObject, type come from? Your are performing an invalid cast, that is why you get the error, this has nothing to with using Load or LoadFrom. Provide more code please. – Georgi Georgiev Sep 19 '19 at 10:02
  • The cast works fine with Load. I will post a minimal working example later. – john_smith_lon Sep 19 '19 at 10:18
  • 1
    Using Load(byte[]) is fraught with trouble. The assembly doesn't have a loading context. An expensive word that means that the CLR doesn't get any help to locate dependent assemblies. It is also quite inefficient, the backing storage for the assembly must be allocated in the paging file and can't come from the memory-mapped file. Don't do it. – Hans Passant Sep 19 '19 at 12:12
  • I was only doing this as I want to have a 'plugin" capability which requires DLLs to be modified and reloaded without restarting the application – john_smith_lon Sep 19 '19 at 14:03

0 Answers0