Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top