Later binding to dll based on processor architecture

I am currently writing a support library that connects to the workshop PLC through the Software Toolbox TopServer .

The TopServer library has separate versions for the x86 and x64 architectures, and I want to download the corresponding version at runtime using late binding based on the processor architecture of the calling code. Methods in both libraries have the same signature.

I can use reflection to load the corresponding object using the code below, but I'm wondering what is the best way to use 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); } } 

Since I'm late, I don't get pre-runtime types, so I thought that creating an interface based on library method signatures would be a good way to implement both versions.

Does anyone have a look at this method or recommendations for other methods?

+6
source share
1 answer

If the target DLLs are changed only according to the target architecture, and the nodes are not very named, an interface is not required.

My suggestion is to call them * _64.dll and * _86.dll, respectively, and choose to compile.

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

0
source

Source: https://habr.com/ru/post/958073/


All Articles