How can I use hot swap for a DLL without recompiling the solution in VS2008?

Our solution has a link to a DLL, which is used to communicate with some hardware via Ethernet. We created a second DLL that can be used to simulate hardware, so our developers do not need the specified equipment on each of their desks.

The software does not need to know if it uses real hardware or a simulator. How can we replace links without having to recompile the solution?

Can we just call both links the same and just register / unregister the correct DLL that we want to link to?

[Refresh] Note. The first DLL is a third-party DLL, which we also do not have access to. The second DLL was created by us in C ++. Our application is written in C #.

+6
source share
4 answers

Make both DLLs implement a common interface (ICommunicateToYourHardware) and encode the application for detecting the DLL through reflection, and not by reference.

Sample Request

The interface in this example is called IAuthenticate, but you can use any interface in it. In this case, I'm really looking for any number of IAuthenticate classes. In your case, if different DLLs are located in the same relative path, the same compilation will be loaded into different DLLs.

// We need a container for the DLL files that we find and then a container for the instances of // the plug ins created from the dlls. In this test we are only really looking for One such plug in // but I prefer to use collections for such things as it allows for a more scalable design. This technique // works for 1, 100, or more plugins and/or methods. private List<string> _DLLS; private List<iAuthenticate> _PlugIns; private int LoadPlugIns(string Path) { /// Within the designated Path (and in all subdirectories) locate all .dll files and put the path /// to these files in a collection. GetDLLS(Path); foreach (string dirName in Directory.GetDirectories(Path)) { LoadPlugIns(dirName); } // For each .dll file, inspect it (using reflection) to determine if it is of the iAuthenticate Type // if it is, create and instance of the object and store it in a collection, and assign a delegate to // invoke its Authenticate method from the host application. foreach (string DLL in _DLLS) { Assembly myAssembly = Assembly.LoadFrom(DLL); Type[] Types = myAssembly.GetTypes(); foreach (Type myType in Types) { Type T = myType.GetInterface("iAuthenticate"); if (T != null) { if (_PlugIns == null) { _PlugIns = new List<iAuthenticate>(); } _PlugIns.Add((iAuthenticate)myAssembly.CreateInstance(myType.FullName)); } } foreach (iAuthenticate iAuth in _PlugIns) { this.Authorize += iAuth.Authenticate; } } return _PlugIns.Count; } private void GetDLLS(string Path) { if (_DLLS == null){_DLLS = new List<string>();} foreach (string filename in Directory.GetFiles(Path, "*.dll")) { _DLLS.Add(filename); } } 

once we have a set of links through reflection, we can call its methods as follows:

  private void btnLogon_Click(object sender, EventArgs e) { try { if (Authorize.Invoke(txtUsername.Text, txtPassword.Text, txtPath.Text)) { this.BackgroundImage = TelefloraDemo.Properties.Resources._189469; this.pnlLogon.Visible = false; MessageBox.Show("You have Logged On!"); } } catch (AuthenticationException aex) { DemoCode.LogException(aex); MessageBox.Show(aex.ToString()); } catch (Exception ex) { DemoCode.LogException(ex); MessageBox.Show("No Authenticator"); } } public delegate bool Authenticate(string Username,string Password,string Path); public event Authenticate Authorize; 

Now, obviously, your mileage may vary, but this should make you successful. (Yes, there are other ways, I like this one ..)

+2
source

The MEF (Managed Extensibility Framework) makes this very easy. Just make sure that your real and mock interfaces export the same contract and use the MEF composition to enter the desired version.

here is a simple example on how to use it.

+2
source

This is from MSDN.

You can redirect the assembly binding to a different version using the entries in the application or machine configuration files. You can redirect links to .NET Framework Assemblies, third-party assemblies or expression assemblies. Each version of .NET. The Framework has the configuration of the machine file and any redirection information in this file affects all applications running under this version of .NET. Framework

0
source

A reference to both classes and the use of the factory class to create the corresponding interfaces. Then you can use some external mechanism: a configuration file, a registry entry, a machine name, an assembly ID, etc., to force the factory to fix the correct interface.

0
source

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


All Articles