سؤال

I am currently writing a helper library that connects to shop floor PLCs via Software Toolbox's TopServer.

The TopServer library has separate versions for x86 and x64 architectures and I want to load the appropriate version at runtime using late binding based on the CPU architecture of the calling code. The methods in the two libraries have the same signatures.

I can use reflection to load the relevant object using the below code but I am wondering what is the best method of using this instance in the calling code.

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);
    }
}

As I'm late binding I don't get the types pre-runtime so was thinking that creating an interface based on the libraries method signatures would be a good way to implement both version.

Does anyone have a view on this method or recommendations on other methods?

هل كانت مفيدة؟

المحلول

If the target DLLs only vary by the target architecture and the assemblies are not strongly named, an interface is not necessary.

My suggestion is to name them *_64.dll and *_86.dll respectively and pick one to compile against.

At runtime, all you need to do is System.Reflection.Assembly.LoadFile the correct file.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top