<分区>
我目前正在编写一个帮助程序库,它通过 Software Toolbox's TopServer 连接到车间 PLC .
TopServer 库针对 x86 和 x64 架构有不同的版本,我想根据调用代码的 CPU 架构使用后期绑定(bind)在运行时加载适当的版本。两个库中的方法具有相同的签名。
我可以使用下面的代码使用反射来加载相关对象,但我想知道在调用代码中使用此实例的最佳方法是什么。
public class LateBinding
{
public static T GetInstance<T>(string dllPath, string fullyQualifiedClassName) where T : class
{
Assembly assembly = System.Reflection.Assembly.LoadFile(dllPath);
Type t = assembly.GetType(fullyQualifiedClassName);
return (T)Activator.CreateInstance(t);
}
}
因为我是后期绑定(bind),所以我没有得到运行前的类型,所以我认为创建一个基于库方法签名的接口(interface)是实现这两个版本的好方法。
有没有人对这种方法有看法或对其他方法有推荐?