简体   繁体   English

c#第三方参考更新原因问题

[英]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). 我有一个用c#编写的应用程序,它使用来自IBM(cwbx)的引用。

Originally using v5 I called the function; 我最初使用v5调用了该函数;

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. 但是,IBM用他们的智慧将功能.PCFile更改为.pcFile,导致我的应用程序中断。

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. 创建两个包装程序项目,其中一个针对每个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". 如果没有,您将在“找不到方法:DatabaseDownloadRequest.get_pcFile”的行中看到MissingMethodException。 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. 如果设置正确,则可以使您的核心项目完全不必引用IBM程序集,并且在您的应用程序启动时只能检查一次异常,然后使所有内容正常运行。

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). 接口实现的集合-引用“新” DLL和(1)。
  3. Another collection of interface implementations - references the "old" DLL and (1.). 接口实现的另一个集合-引用“旧” DLL和(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). 一个工厂,它检查“新”实现中的异常,然后根据检查是否通过而仅返回“新”或“旧”实现-参考(1),(2)和(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. 使用工厂的控制台应用程序-仅引用(1)和(4),不引用(2)或(3),更不用说“旧”或“新” DLL。

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. 构建解决方案后,可以将“旧”或“新” DLL放到输出文件夹中,并且我的控制台应用程序将在下次运行时相应地做出响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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