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

Community
  • 1
  • 1
marc wellman
  • 5,808
  • 5
  • 32
  • 59
  • Just strong name and GAC them then you can just add references to both. No funky reflection required. http://stackoverflow.com/questions/2460542/using-different-versions-of-the-same-assembly-in-the-same-folder –  Jan 27 '16 at 08: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](https://msdn.microsoft.com/en-us/library/1fce0hc8(v=vs.110).aspx) – XAMlMAX Jun 09 '17 at 09:53

1 Answers1

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");
Richard
  • 29,854
  • 11
  • 77
  • 120
  • 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) – marc wellman Jan 27 '16 at 09:30
  • ... 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. – marc wellman Jan 27 '16 at 09:31
  • 1
    The linked answer used Assembly.LoadFile rather than Assembly.LoadFrom - have you tried that? – Richard Jan 27 '16 at 09:32
  • 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.dll`and `A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll` – marc wellman Jan 27 '16 at 10:06
  • The issue in my original post was solved by your comment that suggests to use `LoadFile(string)` rather then `LoadFrom(string)`. If you add this to your answer, I will mark it as answer, because then it would fit my question perfectly. – marc wellman Feb 01 '16 at 08:01
  • 1
    Updated answer to include my earlier comment – Richard Feb 01 '16 at 08:15