Resolve Type
Attempts to resolve a type from a given string name using multiple strategies.
- Tries to resolve the type directly using Type.GetType.
- If that fails, it tries appending common assemblies like mscorlib or Private.CoreLib.
- Also scans all loaded assemblies in the current AppDomain.
- If still unresolved, it falls back to manually mapped aliases for primitive and common system types.
- The type name match is case-insensitive and can be a short alias like “int”, or full like “Int32”.
VB
Public Function ResolveType(typeName As String) As Type
' 1: direct resolution
Dim t As Type = Type.GetType(typeName, throwOnError:=False, ignoreCase:=True)
If t IsNot Nothing Then Return t
' 2: with mscorlib (used by .NET Framework)
t = Type.GetType($"{typeName}, mscorlib", throwOnError:=False, ignoreCase:=True)
If t IsNot Nothing Then Return t
' 3: with System.Private.CoreLib (used by .NET Core and .NET 5+)
t = Type.GetType($"{typeName}, System.Private.CoreLib", throwOnError:=False, ignoreCase:=True)
If t IsNot Nothing Then Return t
' 4: search in loaded references
For Each asm In AppDomain.CurrentDomain.GetAssemblies()
t = asm.GetType(typeName, throwOnError:=False, ignoreCase:=True)
If t IsNot Nothing Then Return t
Next
' 5: manual alias for known types
Select Case typeName.Trim().ToLowerInvariant()
Case "boolean", "bool", "system.boolean" : Return GetType(Boolean)
Case "byte", "system.byte" : Return GetType(Byte)
Case "short", "int16", "system.int16" : Return GetType(Short)
Case "integer", "int", "int32", "system.int32" : Return GetType(Integer)
Case "long", "int64", "system.int64" : Return GetType(Long)
Case "single", "float", "system.single" : Return GetType(Single)
Case "double", "system.double" : Return GetType(Double)
Case "decimal", "system.decimal" : Return GetType(Decimal)
Case "string", "system.string" : Return GetType(String)
Case "date", "datetime", "system.datetime" : Return GetType(Date)
Case Else : Return Nothing
End Select
End Function