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 o开发者_运维百科f 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? 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 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.
You may want to investigate the assemblybinding tags.
http://msdn.microsoft.com/en-us/library/twy1dw1e.aspx
you should go for 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
精彩评论