Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

I have a string such as "Int", "Double", "DateTime" etc. How do I get the fully qualified name from this string?

like image 679
user114385 Avatar asked Aug 28 '10 22:08

user114385


People also ask

How do I get a fully qualified assembly name?

If you know the assembly's file system path, you can call the static (C#) or Shared (Visual Basic) AssemblyName. GetAssemblyName method to get the fully qualified assembly name.

What is fully qualified name in C#?

In C#, the full name of the class starts from its namespace name followed by dot(.) operator and the class name, which is termed as the fully qualified name of the class.

What are fully qualified type names?

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.

What is GetType () name in C#?

GetType(String, Boolean) Gets the Type with the specified name, performing a case-sensitive search and specifying whether to throw an exception if the type is not found. public: static Type ^ GetType(System::String ^ typeName, bool throwOnError); C# Copy.


2 Answers

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

like image 137
Rob Avatar answered Sep 21 '22 16:09

Rob


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
    }
}
like image 24
R. Martinho Fernandes Avatar answered Sep 19 '22 16:09

R. Martinho Fernandes