简体   繁体   English

不同名称空间中的类

[英]Class in different namespaces

I'm using a third party dll. 我正在使用第三方dll。 I'm using a class in some namespace namespace.class . 我在某些名称namespace.class使用了一个类。

In newer versions that class has been moved into another namespace so now it is newnamespace.newpart.class . 在较新的版本中,该类已移至另一个名称空间,因此现在是newnamespace.newpart.class My code needs to support both cases. 我的代码需要支持两种情况。 How to I solve this? 我该如何解决?

If a class exists in two namespaces, it's not the same class. 如果一个类存在于两个命名空间中,则它不是同一类。 It's two different classes. 这是两个不同的类。

There are countless ways to 'support' different classes but it depends entirely on your implementation, ie what you mean by 'support both cases'. “支持”不同类的方法有无数种,但这完全取决于您的实现,即“支持两种情况”的含义。

My first advice would be to ensure your code-base uses the same assembly, so refactor your code so it is consistent and uses the newer version - then this whole problem goes away. 我的第一个建议是确保您的代码库使用相同的程序集,因此重构代码以使其一致并使用较新的版本-这样,整个问题就可以解决。

If that's not desirable, then you'll have to alias it in the files depending on what version is being used, eg: 如果不希望这样,则必须根据所使用的版本在文件中为其加上别名,例如:

Legacy .cs file: 旧版.cs文件:

using ThirdPartyClass = ThirdPartyNamespace1.Class

Newer .cs file: 较新的.cs文件:

using ThirdPartyClass = ThirdParty.OtherNameSpace.Class

Note, you may run in to issues depending on the compatibility between the old and new versions. 请注意,您可能会遇到问题,具体取决于新旧版本之间的兼容性。 You really should consider option 1. And the excuse "I don't want to change it all in my code" is not an excuse (and I'm not inferring it is, just saying :) ) 您确实应该考虑选项1。“我不想在代码中全部更改”的借口不是借口(我不推断这是在说:))

Develop a late binding of the 3rd party dll and check which version it is. 开发第三方dll的后期绑定,并检查它是哪个版本。 Than you can create an new instance of the right object in the right namespace. 比您可以在正确的名称空间中创建正确的对象的新实例。

Here some example code: 下面是一些示例代码:

//load dll
System.Reflection.Assembly myDllAssembly =
    System.Reflection.Assembly.LoadFile("myspeciallibrary.dll");

//create object
Object MyDLLObjectInstance;

//initialize object
if (myDllAssembly.ImageRuntimeVersion == "version2")
{
    MyDLLObjectInstance = (Object)myDllAssembly.CreateInstance("MyDLLNamespace.MyDLLObject");
}
else
{
    MyDLLObjectInstance = (Object)myDllAssembly.CreateInstance("MyDLLNamespace.NewNameSpace.MyDLLObject");
}

At the moment, when your application is linking, namespace has to be known. 目前,当您的应用程序链接时,必须知道名称空间。 So you can't just write app supporting two different dlls in the same way. 因此,您不能以相同的方式编写支持两个不同dll的应用程序。

So after compilation there is no way to change dll. 因此,编译后无法更改dll。

You could try to handle your code to use different classes if one or another is available. 如果一个或另一个可用,则可以尝试处理代码以使用不同的类。

One of the ways you can do this is using MEF. 执行此操作的方法之一是使用MEF。 Write two adapter classes / MEF objects in seperate assemblies, check which third party library you have and load the appropriate adapter class. 在单独的程序集中编写两个适配器类/ MEF对象,检查您拥有哪个第三方库并加载适当的适配器类。

Assuming the class is unique (ie it has moved completely from namespace.class to namespace.newpart.class , you could just include both namespaces... 假设该类是唯一的(即,它已经完全从namespace.class移到namespace.newpart.class ,则可以只包含两个命名空间...

This should compile, but at runtime if the library is swapped out it wont work... 这应该可以编译,但是在运行时,如果库被换出,它将无法工作...

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

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