Reputation: 39
I'm using a third party dll. I'm using a class in some namespace namespace.class
.
In newer versions that class has been moved into another namespace so now it is newnamespace.newpart.class
. My code needs to support both cases. How to I solve this?
Upvotes: 1
Views: 413
Reputation: 2004
Develop a late binding of the 3rd party dll and check which version it is. Than you can create an new instance of the right object in the right namespace.
Here some example code:
//load dll
System.Reflection.Assembly myDllAssembly =
System.Reflection.Assembly.LoadFile("myspeciallibrary.dll");
//create object
Object MyDLLObjectInstance;
//initialize object
if (myDllAssembly.ImageRuntimeVersion == "version2")
{
MyDLLObjectInstance = (Object)myDllAssembly.CreateInstance("MyDLLNamespace.MyDLLObject");
}
else
{
MyDLLObjectInstance = (Object)myDllAssembly.CreateInstance("MyDLLNamespace.NewNameSpace.MyDLLObject");
}
Upvotes: 2
Reputation: 3385
One of the ways you can do this is using MEF. Write two adapter classes / MEF objects in seperate assemblies, check which third party library you have and load the appropriate adapter class.
Upvotes: 0
Reputation: 3127
At the moment, when your application is linking, namespace has to be known. So you can't just write app supporting two different dlls in the same way.
So after compilation there is no way to change dll.
You could try to handle your code to use different classes if one or another is available.
Upvotes: 0
Reputation: 8098
If a class exists in two namespaces, it's not the same class. It's two different classes.
There are countless ways to 'support' different classes but it depends entirely on your implementation, ie what you mean by 'support both cases'.
Upvotes: 3
Reputation: 38825
My first advice would be to ensure your code-base uses the same assembly, so refactor your code so it is consistent and uses the newer version - then this whole problem goes away.
If that's not desirable, then you'll have to alias it in the files depending on what version is being used, e.g:
Legacy .cs file:
using ThirdPartyClass = ThirdPartyNamespace1.Class
Newer .cs file:
using ThirdPartyClass = ThirdParty.OtherNameSpace.Class
Note, you may run in to issues depending on the compatibility between the old and new versions. You really should consider option 1. And the excuse "I don't want to change it all in my code" is not an excuse (and I'm not inferring it is, just saying :) )
Upvotes: 2
Reputation: 2590
Assuming the class is unique (i.e. it has moved completely from namespace.class
to namespace.newpart.class
, you could just include both namespaces...
This should compile, but at runtime if the library is swapped out it wont work...
Upvotes: -1