I’m in the situation that I have to use an unsigned third party .NET DLL in my signed application. So I decided to load that library dynamically at run-time:
var asm = Assembly.LoadFrom("THIRDPARTYASSEMBLY.DLL");
There is a static class in the DLL which I defined an interface for in my application and used GetUninitializedObject() to load the class:
var obj = FormatterServices.GetUninitializedObject(asm.GetType("NAMESPACE.CLASS")) as IMyInterface;
Although the class I’m trying to load is not abstract (It’s a public static class) I get this error at runtime:
System.MemberAccessException was unhandled: Cannot create an
abstract class.
Obviously I can’t use CreateInstance method because the static class doesn’t have any constructors.
So what would you suggest? I want to:
- Invoke a
public staticmethod from that library. (I’m currently usingInvokeMember().) - Get a
Custom Typeproperty. - Handle a few
Custom Eventsin my application.
Thanks in advance.
Instead of trying to call the method dynamically, you could try to strong name the third party dll.
You will need a key to strong name the dll with. You could either use the one you are already using for your own dll, or create a new one for this purpose (using
sn -k).To strong name the already compiled assembly:
ildasm ThirdParty.dll /out:ThirdParty.ililasm ThirdParty.il /dll /key=MyKey.snkThis will result in a new
ThirdParty.dllwhich will be strong named with the key you have specified. You can then add a direct reference to it and call the methods directly as you normally would.