[英]Different type libraries for different versions
in my c# project I'm using a type library of an third party software to communicate that software. 在我的C#项目中,我使用的是第三方软件的类型库来传达该软件。 But when someone uses an older Version of the third party software then I have to load another type library (an older one) how can I handle this?
但是,当有人使用较旧版本的第三方软件时,我必须加载另一个类型库(一个较旧的类型库),该如何处理呢?
And the next thing is, that in the type library may be some changes so my actual code doesnt work (maybe!!) 接下来的事情是,在类型库中可能进行了一些更改,因此我的实际代码不起作用(也许!)
First of all, you should create an Interface, which wraps the functionality of your thirdparty library. 首先,您应该创建一个接口,其中包含第三方库的功能。 If you do so, you can have 2 implementations of that interface.
如果这样做,则可以具有该接口的2个实现。 One for each Version of the third party library.
第三方库的每个版本一个。
public interface IWrapThirdParty {
void ThirdPartyMethodOne();
int ThirdPartyMethodTwo();
}
public class ThirdPartyV1Wrapper : IWrapThirdParty {
public void ThirdPartyMethodOne() {
ThirdPartyV1 obj = new ThirdPartyV1();
obj.ThirdPartyMethodOne();
}
public int ThirdPartyMethodTwo(){
ThirdPartyV1 obj = new ThirdPartyV1();
return obj.ThirdPartyMethodTwo();
}
}
To solve your problem with loading the libraries: 要解决加载库的问题:
You can use the Assembly.Load() method after u figured out, which Version u want to load. 在确定了要加载的版本之后,可以使用Assembly.Load()方法。 But be aware to NEVER load an assembly 2 times.
但是请注意,切勿将装配件加载两次。
https://msdn.microsoft.com/en-us/library/system.reflection.assembly.load(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.reflection.assembly.load(v=vs.110).aspx
To check if the assembly is allready loaded u can use the AppDomain.CurrentDomain.GetAssemblies() Method, which will return all assemblies that are currently loaded into the AppDomain. 要检查程序集是否已全部加载,可以使用AppDomain.CurrentDomain.GetAssemblies()方法,该方法将返回当前已加载到AppDomain中的所有程序集。
https://msdn.microsoft.com/en-us/library/system.appdomain.getassemblies(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.appdomain.getassemblies(v=vs.110).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.