获取没有任何generics信息的类型名称
如果我写:
var type = typeof(List); Console.WriteLine(type.Name);
它会写:
List`1
我希望它只写:
名单
我怎样才能做到这一点? 有没有更聪明的方法来实现它而不必使用Substring
或类似的字符串操作函数?
不,它在名称中包含genericsarity是完全合理的 – 因为它是使名称唯一的部分(当然还有程序集和命名空间)。
这样说: System.Nullable
和System.Nullable
是非常不同的类型。 你不希望混淆两者……所以如果你想丢失信息,你将不得不努力去做。 当然,这并不是很难,可以放在辅助方法中:
public static string GetNameWithoutGenericArity(this Type t) { string name = t.Name; int index = name.IndexOf('`'); return index == -1 ? name : name.Substring(0, index); }
然后:
var type = typeof(List); Console.WriteLine(type.GetNameWithoutGenericArity());
不,它没有,因为“generic-type-string”是类型名称的一部分。
如果有人感兴趣,我为这个问题创建了一些扩展方法,创建了一个更“可读”的字符串
它会产生类似的东西
List[string] outer.inner[other.whatever] IEnumerable[T0] Dictionary[string:int]
在这里测试
public static class TypeEx { public static string GetTypeName(this Type type) { if (type == null) throw new ArgumentNullException(nameof(type)); if (!type.IsGenericType) return type.GetNestedTypeName(); StringBuilder stringBuilder = new StringBuilder(); _buildClassNameRecursiv(type, stringBuilder); return stringBuilder.ToString(); } private static void _buildClassNameRecursiv(Type type, StringBuilder classNameBuilder, int genericParameterIndex = 0) { if (type.IsGenericParameter) classNameBuilder.AppendFormat("T{0}", genericParameterIndex + 1); else if (type.IsGenericType) { classNameBuilder.Append(GetNestedTypeName(type) + "["); int subIndex = 0; foreach (Type genericTypeArgument in type.GetGenericArguments()) { if (subIndex > 0) classNameBuilder.Append(":"); _buildClassNameRecursiv(genericTypeArgument, classNameBuilder, subIndex++); } classNameBuilder.Append("]"); } else classNameBuilder.Append(type.GetNestedTypeName()); } public static string GetNestedTypeName(this Type type) { if (type == null) throw new ArgumentNullException(nameof(type)); if (!type.IsNested) return type.Name; StringBuilder nestedName = new StringBuilder(); while(type != null) { if(nestedName.Length>0) nestedName.Insert(0,'.'); nestedName.Insert(0, _getTypeName(type)); type = type.DeclaringType; } return nestedName.ToString(); } private static string _getTypeName(Type type) { return type.IsGenericType ? type.Name.Split('`')[0]: type.Name; } }