Getting a fully qualified type name from a string .Net C#

It's worth bearing in mind that int isn't a type. It's an alias provided by C# for the type System.Int32. Bearing that in mind, and assuming you only care about core types, you can try:

var typeName = "DateTime";

var systemTypesAssembly = Assembly.Load("mscorlib");

var type = (from t in systemTypesAssembly.GetTypes()
           where t.Name == typeName
           select t).FirstOrDefault();

Console.WriteLine(type.FullName);

As I said, this will not work for Int, but would work for Int32 which is the type it's aliased to. This code also contains no error handling for where, for example, no type is found in mscorlib that matches the one you've entered in typeName


Before I go any further, I'd like to point that there is no class in the .NET Framework base class library named Int. There is System.Int32, that in C# happens to have an alias: int.

You can do this several ways.

A simple but limited way to do this is to have a dictionary mapping strings to the corresponding FQNs:

public static IDictionary<string, string> FqnMap = new Dictionary<string, string>
{
    { "Int", "System.Int32" },
    { "Double", "System.Double" },
    { "DateTime", "System.DateTime" },
};

public string GetFqn(string name)
{
    return FqnMap[name]; // TODO: add error handling
}

A more complex but powerful way would involve using reflection to search all types from a set of assemblies. Something like this:

public class FqnResolver
{
    public FqnResolver(IEnumerable<Assembly> assemblies)
    {
        Assemblies = new List<Assembly>(assemblies);
    }
    public FqnResolver(params Assembly[] assemblies) : this(assemblies as IEnumerable<Assembly>) { }
    public FqnResolver() : this(new Assembly[0]) { }

    public ICollection<Assembly> Assemblies { get; private set; }

    public string GetFqn(string name)
    {
        var candidates = from a in Assemblies
                         from t in a.GetTypes()
                         where t.Name == name
                         select t.FullName;
        return candidates.First(); // will throw if no valid type was found
                                   // and does not count duplicates
    }
}

This is not really doable in an efficient fashion in .Net. In order to get a fully qualified name of a type you will need to have the Type object handy. And in order to get a Type object you typically need an instance, the fully qualified name, or you have to do a bit of searching.

The latter works but is not very efficient as you need to search through every assembly and type in the loaded AppDomain to find the correct answer.

foreach ( var assembly in AppDommain.CurrentDomain.GetAssemblies() ) {
  foreach ( var type in assembly.GetTypes() ) {
    if ( type.Name == target ) {
      return type.FullName;
    }
  }
}

This solution still won't 100% work for you though. Primarily because there is no type "Int" in the .Net framework. It's actual type name is "Int32".


A type name like "String" can be ambiguous since it could refer to System.String or FooAssembly.String, so you have to search through all types in all loaded assemblies, which could yield multiple results. This bit of reflection could be slow, but you could do the obvious optimization of caching the results at the expense of memory:

static void Main(string[] args)
{
    string typeName = "String";
    foreach (var type in GetFullType(typeName))
    {
        Console.WriteLine(type.FullName);
    }
}

static IEnumerable<Type> GetFullType(string className)
{
    foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
    {
        foreach (var type in assembly.GetTypes())
        {
            if (type.Name.Equals(className, StringComparison.OrdinalIgnoreCase))
            {
                yield return type;
            }
        }
    }
}

Comments

  1. Miguel

    • 2018/10/7

    It's worth bearing in mind that int isn't a type. It's an alias provided by C# for the type System.Int32 . Bearing that in mind, 

  2. John

    • 2015/5/13

    This is not really doable in an efficient fashion in .Net. In order to get a fully qualified name of a type you will need to have the Type object handy. And in order to get a Type object you typically need an instance, the fully qualified name, or you have to do a bit of searching. The latter works but is not very efficient as you need to search through every assembly and type in the loaded AppDomain to find the correct answer.

  3. Bruno

    • 2015/5/22

    Gets the fully qualified name of the type, including its namespace but not its assembly. String // Assembly Qualified Name: System.String, mscorlib 

  4. Remi

    • 2020/3/21

    Specify generic types. Specify pointers. Specify references. Specify arrays. See also. You must specify type names to have valid input to various reflection operations. A fully qualified type name consists of an assembly name specification, a namespace specification, and a type name. Type name specifications are used by methods such as Type.GetType, Module.GetType, ModuleBuilder.GetType, and Assembly.GetType.

  5. Mitchell

    • 2017/10/21

    Also called a zap string. The following example shows an AssemblyName for a simply named assembly with default culture. C# Copy.

  6. Dariel

    • 2017/11/17

    The AssemblyQualifiedName property returns a fully qualified type name including nested types, the assembly name, and generic arguments. All compilers that support the common language runtime will emit the simple name of a nested class, and reflection constructs a mangled name when queried, in accordance with the following conventions.

  7. Arian

    • 2018/4/22

    If you happen to have the fully assembly-qualified type name including the assembly you can simply get it with. Type.GetType(fullyQualifiedName);.

  8. Pellegrini

    • 2016/7/28

    1.) Given only a string containing fully qualified type name (e.g. "System.Drawing.Rectangle"), is there a consistent way to get the Type object that the string corresponds to that will work with most types? 2.) If no to question 1, then given the string containing the fully qualified type name, is there a way to get the fully qualified assembly

  9. De Santis

    • 2018/7/6

    In computer programming, a fully qualified name is an unambiguous name that specifies which object, function, or variable a call refers to without regard to 

  10. Williams

    • 2016/3/13

    I've been able to get some fully qualified names by iterating over the Types within the assemblies by AppDomain.CurrentDomain.GetAssemblies () or by Assembly.GetCallingAssembly.GetAssemblies () but I suspect that I'm not looking in the right place to find the Process namespace (even though System.dll is in the Current Domain).

  11. Collin

    • 2018/6/7

    The namespace \Foo is also a fully qualified name. Relative name. This is an identifier starting with if not found, it tries to autoload class "C\E"

  12. Diaz

    • 2015/6/6

    This method returns the fully qualified common language runtime namespace and name for all primitive types. For example, the C# instruction, (long)0.Type ().ToString () returns "System.Int64" instead of merely "Int64".

  13. Huxley

    • 2016/4/22

    Fully qualified and canonical names (§6.7) are also discussed in this chapter. The declaration of a type which is not generic ( class C ..

  14. Kendrick

    • 2020/1/10

    The Type.GetType() is an overloaded method and accepts the following parameters: fully qualified string name of the type you are interested in 

  15. Ryland

    • 2015/3/7

    both expose a specific type with equal name and equal namespace. across assemblies with the same fully-qualified type names in C#.

  16. Finn

    • 2020/4/4

    C# / C Sharp Forums on Bytes. Does anyone know how to extract the full type name assembly-qualified strings themselves.

Comments are closed.

Recent Posts