This is a follow-up to my question here.
I want to load the same assembly in different versions and create multiple instances of types from these assemblies.
Here is what I have:
I have an assembly asm.dll whos version (within AssemblyInfo.cs) is set to 1.0.0.0.
Then, I modify some code and increment the version to 2.0.0.0 and build it again as asm.dll.
Now, I have dir1/asm.dll and dir2/asm.dll.
Here is what I do:
assembly = Assembly.LoadFile(assemblyFile);
var types = assembly.GetTypes();
Type type = types.First<Type>(t => t.Name.Equals(backboneMemberClass + "Editor"));
MyObject myObject = (MyObject)assembly.CreateInstance("theClassIWantToInstantiate", false, BindingFlags.CreateInstance, null, new object[] { }, null, null);
Here is the problem:
The above works fine if I use "dir1/asm.dll" as assemblyFile: The call to assembly.CreateInstance(...)
returns me the requested instance.
If I use it again wit "dir2/asm.dll" it still works fine. Assembly.CreateInstance
returns the correct instance.
BUT, if I then again want to create an instance of an object I did already create before (through calling Assembly.CreateInstance
) I am getting the following Exception(s):
A first chance exception of type 'System.Exception' occurred in PresentationFramework.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
The class I am loading is a .xaml WPF UserControl and the Stacktrace of the Exception says that the InitializeComponent() within the .xaml.cs file is throwing the Exception because it cannot find the .baml file.