Set referenced DLL path at runtime
Is it possible to change the path of a DLL at runtime? I need to link a DLL, but I will not be able to give the DLL to the user, but i know the user have that DLL on it's hard disk. So i have show a generic form to the user, where it can enter the path,开发者_运维问答 then I have to take that path (as string) and load the dll dynamically to my program. After it's loaded I will use stuff from that DLL.
Is this possible and even if, how to do?
To add to Chris' answer.
To avoid using reflection on the loaded assembly, code your classes within the assembly against common interfaces and you can then instantiate your objects like so:
Assembly assembly = Assembly.LoadFile(pathOfAssembly);
InterfaceName instance = (InterfaceName)assembly.CreateInstance("fully qualified type name", true);
You can load an assembly or dll dynamically from path using Assembly.LoadFile(pathOfAssembly)
method. Once it is loaded you can create instances of classes it contains and call methods.
精彩评论