简体   繁体   中英

c# Third party Reference Update Cause Issues

I was wondering if you would help.

I have an app written in c# that uses a reference from IBM (cwbx).

Originally using v5 I called the function;

var download = new DatabaseDownloadRequest
                   {
                     system = host,
                     UseCompression = true
                     };

  download.AS400File.Name = tableNames;
  tempFileName = Path.GetTempFileName();
  fileDescriptionFile = Path.GetTempFileName();

  download.PCFile.Name = tempFileName;
  download.PCFile.FileType = delimiterType;

However, IBM in their wisdom have changed the function .PCFile to .pcFile, causing my app to break.

This would not be an issue if all users within my company used the latest version, but there will be a slow uptake on this, so I need to be able to use both.

Is there any sort of function where I can upper or lower case the function that I need to use so that it will be the same, or can you think of any other way I can do this?

Thanks,

Dave

I'd recommend wrapping these calls into a class that you've created which accesses them using reflection. I can't imagine any other approach is going to result in anything stable.

I think using reflection is your best solution here.

Something like this should work with either version of the library:

PropertyInfo pcFileProperty = download.GetType().GetProperty("PCFile");

if (pcFileProperty == null)
{
    pcFileProperty = download.GetType().GetProperty("pcFile");
}

if (pcFileProperty != null)
{
    PCFileType file = (PCFileType)pcFileProperty.GetValue(download, null);
    file.Name = tempFileName;
    file.FileType = delimiterType;
}
else
{
    // Property not found - IBM has changed the API again?
    // Throw an exception?
}

This may be a roundabout way of doing it, but I've tried the following with some dummy classes and it seems to work:

  • Create two wrapper projects, one built against each version of the DLL.
  • In your main project, try to call the new version of the method via the corresponding wrapper projects. If it works, great. If not, you'll get a MissingMethodException along the lines of "Method not found: DatabaseDownloadRequest.get_pcFile". Catch this exception and use the other class instead!

If you set this up right, you can make your core project not have to reference the IBM assembly at all, and you'll be able to check for the exception only once when your application starts, and thereafter have everything run as normal.

To clarify, I have five projects:

  1. A collection of interfaces giving what I need.
  2. A collection of interface implementations - references the "new" DLL and (1).
  3. Another collection of interface implementations - references the "old" DLL and (1.).
  4. A factory, that checks for an exception in a "new" implementation and thereafter eturns only "new" or "old" implementations depending on if the check passed - references (1), (2) and (3).
  5. A console application using the factory - only references (1) and (4), doesn't reference (2) or (3) let alone the "old" or "new" DLLs.

Once I have built the solution, I can drop the "old" or "new" DLL into my output folder and my console app will repond accordingly next time I run it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM