Hi,

I have a windows application which uses reflection and factory pattern. The
code in the factory class to create an instance of an assembly is:

dim assemblyInstance as [Assembly] = [Assembly].LoadFrom(pAssembly)
dim typeInstance as Type = assemblyInstance.GetType(pType)
Dim obj As Object = Activator.CreateInstance(typeInstance)

where pAssembly is the path of the .dll and the pType is the type of the
assembly.

When running the application, the above code will be invoked twice for two
different assemblies. For the first one, it works perfect. But for the second
one, the CreateInstance statement throws
System.Reflection.TargetInvocationException. The innerException is
System.InvalidCastException with message "Specified cast is not valid."

What's wrong? Thanks.