views:

55

answers:

2

We have a product that uses a Reference from a 3rd party supplier. With the release of their new product they have renamed this Reference to a new name.

What we want to do is have the one version of our application compiled so that it can run against both the old and new names of the library.

There is basically no change, only 1 method rename, between the parts of the library we use, but I have no idea how to develop our application to handle both.

If I have to we can branch the code to work with both, but I'd really like to have some sort of adapter that all calls go through that then dispatches to either the old or new.

Once I install the new application, it removes the old library, so the old code won't compile.

Any pointers on what I can try or how I can work around this issue?

Also, the application is developed in C# using Visual Studio 2005.

+1  A: 

I would suggest you the following steps:

  • Refactor your code to isolate the call to the library by having your own class which inherits from the library (no own methods for now - just one single place of change)
  • Use Reflection inside this single class to find out which of the two method is available. Cache the actual method to avoid a performance penalty when you call the library very frequently. The actual reflection will look like this:

    Type type = Type.GetType(alien); MemberInfo[] mbrInfoArray=type.GetMethods();

The method will be called using Invoke.

weismat
+3  A: 

Look at Assembly Binding Redirection... You can redirect the old DLL references to the new one. You will need to write a wrapper method for the renamed method.. That's a real pain in the butt. I'm gonna spitball this off the top of my head, so I don't guarantee name accuracy or compilability, but you can consider it pseudo code...

private bool _useOldMethodName = false;
public void MethodAlias(string arg1)
{
    if (_useOldMethodName)
    {
        Reference.OldFunctionName(arg1);
    }
    else
    {
        try
        {
            Reference.NewFunctionName(arg1);
        }
        catch (MethodNotFoundException mnfe)
        {
            _useOldMethodName = true;
        }
    }
}

Something like that. It's not ideal in any case.

I am curious, why can't you just always use the new reference? Just distribute the new reference DLL with your code, and you'll never have an issue...

Christopher Karper
We don't control the application this library is in, and its part of a much bigger system. We have an addon that talks via its offered API to do some functions.I have played around with installing the old and new dll's on the same machine and they do seem to work, but that was only a small test and I'm not sure if it will cause more harm than good.I like redirection idea and will give it a spin and see what I can come up with. Thanks.
stevemac