문제

I have an application where I want to instantiate a class that is completely outside the application, perhaps written at a later date by a third party. So the class cannot be known, but the interfaces in the class are known. So I want to use late binding.

In my code (VB.NET), I have the following:

Dim a As Object
a = Activator.CreateInstance("MyNameSpace.CustomClass", "")
MsgBox(a.Name)

I get an exception at the second line: Could not load file or assembly 'MyNameSpace.CustomClass' or one of its dependencies. The system cannot find the file specified. even though the assembly is in the same folder as the executable. I can't use Type.GetType() because the type is not known to the calling assembly.

도움이 되었습니까?

해결책

You need the CreateInstanceFrom method.

    var typeReference = Activator.CreateInstanceFrom(assemblyPath, 
                                                       fullyQualifiedClassName);

But, for me, MEF would be a better solution as you can bind the Import/Export on the interface.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top