简体   繁体   English

引用两个程序集之一中可能存在的类

[英]Referencing a class that may exist in one of two assemblies

I am currently working on a data harvester for a project. 我目前正在为一个项目开发数据收集器。 The way it works is it basically calls another program which collects the data, and that program returns a data structure containing a collection of queue information. 它的工作方式是基本上调用另一个收集数据的程序,该程序返回包含队列信息集合的数据结构。 The harvester then serializes out the data. 然后,收割机将数据序列化。 The program that actually collects the data is maintained by another team, and recently they did an upgrade and decided to restructure their code. 实际收集数据的程序由另一个团队维护,最近他们进行了升级,并决定重组其代码。 The method I am calling is still in the same location, however, the data structure I get back got moved to a different assembly (it's code remained the same). 我要调用的方法仍在同一位置,但是,我取回的数据结构移到了另一个程序集中(其代码保持不变)。 The fun part of this is that we have both versions of this product in the field right now, so depending on which version a client has, the data structure I need may be in one of two different assemblies. 有趣的部分是,我们现在在现场拥有该产品的两个版本,因此,根据客户端的版本,我需要的数据结构可能位于两个不同的程序集中的一个中。 My boss wants to try to only have one version of the harvester program if possible. 我的老板想尽可能地尝试只拥有一个版本的收割机程序。

So, my question is this: using C# and .NET 3.5, is there a way to pick which assembly to use at runtime? 所以,我的问题是这样的:使用C#和.NET 3.5,是否可以选择在运行时使用哪个程序集? I think I could use reflections, but I'd like to know if there's any way I could write the code normally for compile time and then resolve the dependency at run time depending on the other program's version. 我想我可以使用反射,但是我想知道是否有什么方法可以正常地在编译时编写代码,然后根据其他程序的版本在运行时解析依赖关系。

you should go for AppDomain 你应该去AppDomain

http://www.codeproject.com/KB/cs/Assemblies___Appdomains.aspx

And another one is most popular http://www.west-wind.com/Weblog/posts/601200.aspx 另一个是最受欢迎的http://www.west-wind.com/Weblog/posts/601200.aspx

You may want to investigate the assemblybinding tags. 您可能要研究assemblybinding标记。

http://msdn.microsoft.com/en-us/library/twy1dw1e.aspx http://msdn.microsoft.com/en-us/library/twy1dw1e.aspx

You could try using the Adapter (wrapper) design pattern. 您可以尝试使用适配器(包装器)设计模式。

interface IQueueInfoProvider
{
   DataStructure FetchData();
}

class Version1QueueInfoProvider : IQueInfoProvider
{
   DataStructure FetchData()
   {
      //Fetch using Version1 Assemblies.
   }
}

class Version2QueueInfoProvider : IQueInfoProvider
{
   DataStructure FetchData()
   {
       //Fetch using Version2 Assemblies.
   }
}

I believe .NET won't attempt to load referenced assemblies if they aren't needed, but if i'm wrong, you can always use reflections. 我相信.NET不会在不需要的情况下尝试加载引用的程序集,但是如果我错了,则可以随时使用反射。

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

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