从柯克的角度构建一个更完整的答案看起来像这样。修改:
- 支持所有 C# 关键字
- 支持自定义翻译
- 数组
- Nullables
ValueType?
代替Nullable<ValueType>
这是完整的代码:
public static class TypeTranslator
{
private static Dictionary<Type, string> _defaultDictionary = new Dictionary<System.Type, string>
{
{typeof(int), "int"},
{typeof(uint), "uint"},
{typeof(long), "long"},
{typeof(ulong), "ulong"},
{typeof(short), "short"},
{typeof(ushort), "ushort"},
{typeof(byte), "byte"},
{typeof(sbyte), "sbyte"},
{typeof(bool), "bool"},
{typeof(float), "float"},
{typeof(double), "double"},
{typeof(decimal), "decimal"},
{typeof(char), "char"},
{typeof(string), "string"},
{typeof(object), "object"},
{typeof(void), "void"}
};
public static string GetFriendlyName(this Type type, Dictionary<Type, string> translations)
{
if(translations.ContainsKey(type))
return translations[type];
else if (type.IsArray)
{
var rank = type.GetArrayRank();
var commas = rank > 1
? new string(',', rank - 1)
: "";
return GetFriendlyName(type.GetElementType(), translations) + $"[{commas}]";
}
else if(type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
return type.GetGenericArguments()[0].GetFriendlyName() + "?";
else if (type.IsGenericType)
return type.Name.Split('`')[0] + "<" + string.Join(", ", type.GetGenericArguments().Select(x => GetFriendlyName(x)).ToArray()) + ">";
else
return type.Name;
}
public static string GetFriendlyName(this Type type)
{
return type.GetFriendlyName(_defaultDictionary);
}
}