如何在同一组件的不同版本之间切换

问题描述:





我有两个不同的程序集具有相同的名称和命名空间。有任何解决方案可以避免在添加两个dll的引用时发生冲突。



我们可以使用配置文件在同一个程序集的两个版本之间切换。





在我的项目中,我在两个不同的场景中使用这两个dll。我们可以在执行之前指定dll版本。



对于第一种情况,我使用的是版本1.0,在第二种情况下使用版本2.0。所以可以在运行时在这两个程序集之间切换。



// Dll版本1.0

namesapace测试

{

班级连接

{

int connectTO()

{

//从数据表访问静态数据

}

}

}







// Dll Version 2.0

namesapace测试

{

class connect

{

int connectTO()

{

//从数据库访问数据

}

}

}

Hi,

I have 2 different assembly with same name and namespace.Is there any solution to avoid conflict at the time of adding references of both the dlls.

Can we switch between 2 versions of same assembly using config file.


In my project, I am using these two dlls in two different scenarios.so can we specify dll version before execution.

For first scenario I am using version 1.0 and in second scenario using version 2.0.So is it possible to switch between these two assemblies at run time.

// Dll Version 1.0
namesapace Test
{
class connect
{
int connectTO()
{
// access static data from datatable
}
}
}



// Dll Version 2.0
namesapace Test
{
class connect
{
int connectTO()
{
//access data from database
}
}
}

假设程序集具有不同的名称,您可能可以使用extern别名:

http://blogs.msdn。 com / b / ansonh / archive / 2006/09/27 / 774692.aspx [ ^ ]
Assuming the assemblies have different names, you might be able to use an "extern alias":
http://blogs.msdn.com/b/ansonh/archive/2006/09/27/774692.aspx[^]


解决方案1是一个非常好的答案。实际上,这是最不为人知的功能之一,很少使用,真的很难发现,真的可以用来解决相同组件附近的引用。



但它应该是用于解决同一组件的不同版本,同时使用两者?是的,这是可能的,但只能算是最后的手段。如果两个版本都属于开发团队,或者可以以某种方式获得源代码,那么最好将它们合并为一个,但通常情况下,没有必要。在几乎所有情况下,它已经在最新版本中完成,因此使用旧版本的需求也只是神话般的。只需要迁移到一个版本就可以了。



一次使用两个或更多选项支持代码似乎是一个解决方案,但即使有开发不足时间它不可能是一个可行的解决方案,因为它只会在短时间内解决问题,使事情变得更糟。请重新考虑一下,不要给自己造成大问题。







现在,来自原始MSDN源的详细信息: http://msdn.microsoft.com/en-us/library/ ms173212.aspx [ ^ ]。



本文档无法解释所有语法细节。首先,此声明必须位于所有使用子句之上。



参见: http://www.dotnetperls.com/extern [ ^ ]。



-SA
Solution 1 is a really excellent answer. Indeed, this is the one of least known features, rarely used, really hard to discover, really can be used to resolve referencing near identical assemblies.

But should it be used to resolve different versions of same assemblies, to use both at the same time? Yes, this is possible, but can only be considered as a last resort. If both versions belong to the development teams, or source codes can be obtained somehow, it would be the best to merge them in one, but usually, there is no a need in that. In nearly all cases, it is already done in the latest version, so a need for using a legacy version, too, would be just mythical. All it takes should be done to migrate to only one version.

Supporting code using two or more options at a time may seem a solution, but even with deficit of development time it cannot be a viable solution, because it post-pones the problem only for short period of time, to make thing a lot worse. Please rethink it all, don't create yourself a big problem.



Now, the detail from original MSDN source: http://msdn.microsoft.com/en-us/library/ms173212.aspx[^].

This documentation fails to explain all the syntax detail. First of all, this declaration is required to be on top of all the uses clauses.

See also: http://www.dotnetperls.com/extern[^].

—SA