2

I have one assembly that is called asm.dll.

This assembly has the version 1.0.0.0 (set within AssemblyInfo.cs)

Then I need do do some code modifications in that assembly (still asm.dll), advance the version to 2.0.0.0 and build it again.

Now, I have two files named asm.dll that differ with respect to some code modifications and a their version number.

How do I load these two files during runtime?

ADDENDUM:

Right now I am trying the following:

var asm1 = Assembly.LoadFrom("dir1\asm.dll"); var asm2 = Assembly.LoadFrom("dir2\asm.dll"); var types1 = asm1.GetTypes(); var types2 = asm2.GetTypes(); Type type1 = types1.First<Type>(t => t.Name.Equals("myClassIWantToInstantiate")); Type type2 = types2.First<Type>(t => t.Name.Equals("myClassIWantToInstantiate")); MyObject myObject1 = (MyObject1)Activator.CreateInstance(type, new object[] { }); MyObject myObject2 = (MyObject2)Activator.CreateInstance(type, new object[] { }); 

But I get the following behavior:

  • the first call to Activator.CreateInstance(...) correctly returns the requested instance for myObject1

  • the second call to Activator.CreateInstance(...) returns again myObject1 instead of myObject2

  • The code compiles and the program runs without exception or observable problems, except that I do not get myObject2

I am aware of this answer and I think the code I used, is the same, only a bit newer (correct me, if I am wrong).

2
  • Just strong name and GAC them then you can just add references to both. No funky reflection required. stackoverflow.com/questions/2460542/… Commented Jan 27, 2016 at 8:41
  • Bit late to the party but I had similar problem, I had to load in 2 instances of the same dll from the same folder (different names), what worked for me was ObjectHandle from Activator.CreateInstanceFrom(string,string) and then Unwrap() to get the object. MSDN Docs Commented Jun 9, 2017 at 9:53

1 Answer 1

2

In your answer, you are using Activator.CreateInstance for both objects - this is using whatever is registered globally. I believe the types loaded from the specific assemblies will not be enough to do this.

In the answer you linked, the assemblies are loaded using Assembly.LoadFile rather than LoadFrom, and CreateInstance is called on the assembly instance, rather than using the static Activator.CreateInstance. Have you tried this?

var asm1 = Assembly.LoadFile("dir1\asm.dll"); var asm2 = Assembly.LoadFile("dir2\asm.dll"); MyObject myObject1 = (MyObject)asm1.CreateInstance("myClassIWantToInstantiate"); MyObject myObject2 = (MyObject)asm2.CreateInstance("myClassIWantToInstantiate"); 
Sign up to request clarification or add additional context in comments.

6 Comments

No, i just checked it. It seems that the problem lies at var asm = Assembly.LoadFrom(assemblyFile); because here the second call still return the same assembly (even though the passed file string is different)
... that means that in my code both calls var asm1 = Assembly.LoadFrom("dir1\asm.dll"); and var asm2 = Assembly.LoadFrom("dir2\asm.dll"); do return the same assembly.
The linked answer used Assembly.LoadFile rather than Assembly.LoadFrom - have you tried that?
In fact, that works! But this seems to introduce another issue because now it seems that I cannot repeat the instantiation of myObject. The first call to asm1.CreateInstance(..) works, the seconds call works too, but if I do again call asm1.CreateInstance(..) I get the following Exceptions: A first chance exception of type 'System.Exception' occurred in PresentationFramework.dlland A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
Updated answer to include my earlier comment
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.