Take the 2-minute tour ×

What is the best way to convert a string into a Type object in .NET?

Issues to consider:

  • The type may be in a different assembly.
  • The type's assembly may not be loaded yet.

This is my attempt, but it doesn't address the second issue

Public Function FindType(ByVal name As String) As Type
    Dim base As Type

    base = Reflection.Assembly.GetEntryAssembly.GetType(name, False, True)
    If base IsNot Nothing Then Return base

    base = Reflection.Assembly.GetExecutingAssembly.GetType(name, False, True)
    If base IsNot Nothing Then Return base

    For Each assembly As Reflection.Assembly In _
        base = assembly.GetType(name, False, True)
        If base IsNot Nothing Then Return base
    Return Nothing
End Function
share|improve this question
Addressing the second case is difficult. How to know generically where the unloaded assemblies are residing? Otherwise see this answer, quite good. –  nawfal Dec 5 '13 at 19:44

2 Answers 2

up vote 2 down vote accepted

you might need to call GetReferencedAssemblies() method for the second.

namespace reflectme
    using System;
    public class hello
        public hello()
        static void Main(string[] args)
            Type t = System.Reflection.Assembly.GetExecutingAssembly().GetType("reflectme.hello");
share|improve this answer
That sounds more like what I'm looking for. –  Jonathan Allen Mar 3 '09 at 22:09

You can use Type.GetType(string) in order to do this. The type name must be assembly qualified but the method will load the assembly as necessary. The assembly qualification is not necessary if the type is in mscorlid or the assembly which executes the GetType call.

share|improve this answer
Note that this does not throw an exception if the type is not found, it returns null. If you are expecting the type to exist then it's worth using the Type.GetType(string,bool) overload and passing true, which throws if the type cannot be loaded. –  Greg Beech Mar 3 '09 at 21:50
"assembly qualified" means you have to put the FULL type name, like this: "NLog, Version=, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL" –  Orion Edwards Mar 3 '09 at 22:01
That doesn't help me much, there is zero chance of me having fully qualified names. –  Jonathan Allen Mar 3 '09 at 22:06

Your Answer


