Step 1: Create interfaces for the objects you're using in EmguCv
and place these in a separate AnyCpu
project. If there is no common interface for these objects, or when you cannot alter these objects, you'll need to create your own wrapper objects for these classes. It is important that these interfaces are defined in their own assembly, since you'll be referencing this assembly from all your own code.
public interface IEmguCv{ /* methods and proeprties */ }
public class EmguCv : IEmguCv { }
Now in your existing application, use Reflection or Activator.CreateInstance
to load the correct assembly:
// which assembly to load depends on your configuration
Assembly emguAssembly = Assembly.Load("EmguX64");
Type emguType = emguAssembly.GetType("EmguCvClassYouAddedInterfacesTo");
IEmguCv object = (IEmguCv)Activator.CreateInstance(emguType);
From here you can use the IEmguCv interface, which will provide you with strong binding, IntelliSense and all the other things you'd expect, but you will be able to load the assembly dynamically based on the context you're under.
Should you need to call a non-default constructor on the EmguCv object, then you can either use the Activator.CreateInstance(Type, Object[], Object[])
overload, or call the constructor through Type.GetConstructor(Type[]).Invoke()
.
You could also use a library such as Unity or NInject once you have an interface defined, so that you can use these DI containers to do the reflection work for you.