I'm connecting my program to some external code. I'm setting it up so that the external code can instance objects and I've come across a problem. I've created this function here:
Public Function InstanceOf(ByVal typename As String) As Object
Dim theType As Type = Type.GetType(typename)
If theType IsNot Nothing Then
Return Activator.CreateInstance(theType)
End If
Return Nothing
End Function
I'm trying to create a System.Diagnostics.Process
object. For what ever reason though, it always return Nothing
instead of the object. Does anybody know what I'm doing wrong?
I'm doing this in VB.net so all .net responses are accepted :)
Read carefully through the documentation of Type.GetType()
, specifically, this part:
If typeName includes the namespace but not the assembly name, this method searches only the calling object's assembly and Mscorlib.dll, in that order. If typeName is fully qualified with the partial or complete assembly name, this method searches in the specified assembly. If the assembly has a strong name, a complete assembly name is required.
Since System.Diagnostics.Process
is in System.dll (not Mscorlib.dll), you need to use the fully qualified name. Assuming you're using .Net 4.0, that would be:
System.Diagnostics.Process, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
If you don't want to work with fully qualified names, you can go through all loaded assemblies and try to get the type using Assembly.GetType()
.