I have a C# VS2017 project that uses third-party assembly Foo.dll version 2.0.0.0. Now Foo.dll 3.0.0.0 has been released and I would like to allow the user to switch between them at runtime because version 3 is not backwards compatible.
This is what I have so far:
Type V2 = Assembly.Load("foo, Version=2.0.0.0, Culture=neutral, PublicKeyToken=7f709c5b713576e1, processorArchitecture=MSIL");
Type V3 = Assembly.Load("foo, Version=3.0.0.0, Culture=neutral, PublicKeyToken=7f709c5b713576e1, processorArchitecture=MSIL");
Type BarV2Type = V2.GetType('Foo.Baz.Bar');
Type BarV3Type = V3.GetType('Foo.Baz.Bar');
var BarV2 = Activator.CreateInstance(BarV2Type);
var BarV3 = Activator.CreateInstance(BarV3Type);
I now need to call the function CreateEngine()
on BarV2 or BarV3, depending on what the user chooses. However BarV2/BarV3 are object type. It seems to cast them to the actual type of 'Bar' I need to add a reference to the assemblies, but I can't do that because they have the same name.
The class Bar is not derived from any interface or class in either assembly.
I then tried adding a reference to Foo.dll V2 and changing its Alias to 'foov2' so I could add a reference to Foo.dll V3 but VS2017 stopped me from adding it, complaining 'Foo' was already added to the project.
Is what I want to do possible? I am thinking that I need to get the CreateEngine()
function using reflection and call it via that, but am I just going to create endless problems for myself with this general approach?