Your question isn't entirely clear: I'm not sure what form you've got for the C# alias. If you know it at compile time, you can use typeof()
as normal - the C# aliases really are just aliases, so typeof(int) == typeof(System.Int32)
. There's no difference in the code emitted.
If you've got a string, e.g. "int"
, just build a map:
Dictionary<string,Type> CSharpAliasToType = new Dictionary<string,Type>
{
{ "string", typeof(string) },
{ "int", typeof(int) },
// etc
};
Once you've got the Type
you can get the full name, the assembly etc.
Here's some sample code which takes into account nullable types:
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;
}