Getting .Net type of corresponding C # type through reflection

there is a function that, when the string representation of the C # type, returns the corresponding string representation of the .Net or .Net type; or in any way to achieve it.

For example:

"bool" → System.Boolean or "System.Boolean"

"int" → System.Int32 or "System.Int32"

...

Thank.

Edit: very sorry, this is not a type-to-type mapping that I want, but either a "string to string" mapping or a "string to type" mapping.

+2


source to share


3 answers


The list of built-in types in C # is pretty short and not likely to change, so I think having a dictionary or large switch

to map them shouldn't be difficult to maintain.

If you want to support nullable types, I believe you have no choice but to parse the input string:

static Type GetTypeFromNullableAlias(string name)
{
    if (name.EndsWith("?"))
        return typeof(Nullable<>).MakeGenericType(
            GetTypeFromAlias(name.Substring(0, name.Length - 1)));
    else
        return GetTypeFromAlias(name);
}

static Type GetTypeFromAlias(string name)
{
    switch (name)
    {
        case "bool": return typeof(System.Boolean);
        case "byte": return typeof(System.Byte);
        case "sbyte": return typeof(System.SByte);
        case "char": return typeof(System.Char);
        case "decimal": return typeof(System.Decimal);
        case "double": return typeof(System.Double);
        case "float": return typeof(System.Single);
        case "int": return typeof(System.Int32);
        case "uint": return typeof(System.UInt32);
        case "long": return typeof(System.Int64);
        case "ulong": return typeof(System.UInt64);
        case "object": return typeof(System.Object);
        case "short": return typeof(System.Int16);
        case "ushort": return typeof(System.UInt16);
        case "string": return typeof(System.String);
        default: throw new ArgumentException();
    }
}

      



Test:

GetTypeFromNullableAlias("int?").Equals(typeof(int?)); // true

      

+6


source


Your question is not entirely clear: I'm not sure what form you have for a C # alias. If you know this at compile time, you can use typeof()

as usual - C # aliases are really just aliases, so typeof(int) == typeof(System.Int32)

. There is no difference in the emitted code.

If you have a string like. "int"

, just build the map:

Dictionary<string,Type> CSharpAliasToType = new Dictionary<string,Type>
{
    { "string", typeof(string) },
    { "int", typeof(int) },
    // etc
};

      



Once you get it Type

, you can get the full name, assembly, etc.

Here is some sample code that takes NULL types into account:

public static Type FromCSharpAlias(string alias)
{
    bool nullable = alias.EndsWith("?");
    if (nullable)
    {
        alias = alias.Substring(0, alias.Length - 1);
    }
    Type type;
    if (!CSharpAliasToType.TryGetValue(alias, out type))
    {
         throw new ArgumentException("No such type");
    }
    return nullable ? typeof(Nullable<>).MakeGenericType(new Type[]{ type })
                    : type;
}

      

+3


source


Don't be so hard.

try

typeof(bool).ToString()
typeof(string).ToString()
typeof(int).ToString()
typeof(char).ToString()

      

...

+1


source







All Articles