晚期基于CPU架构绑定到dll

Phi*_*ray 6 .net c# reflection late-binding

我目前正在编写一个帮助库,通过Software Toolbox的TopServer连接到车间PLC .

TopServer库有x86和x64架构的单独版本,我想在运行时使用基于调用代码的CPU架构的后期绑定来加载适当的版本.两个库中的方法具有相同的签名.

我可以使用反射来使用下面的代码加载相关对象,但我想知道在调用代码中使用此实例的最佳方法是什么.

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

由于我是后期绑定,我没有获得运行前的类型,所以认为创建基于库方法签名的接口将是实现这两个版本的好方法.

有没有人对这种方法或其他方法的建议有看法?

Gus*_*dor 0

如果目标 DLL 仅因目标体系结构而异,并且程序集没有命名,则不需要接口。

我的建议是将它们分别命名为 *_64.dll 和 *_86.dll 并选择一个进行编译。

在运行时,您所需要做的就是System.Reflection.Assembly.LoadFile正确的文件。