当type.IsGenericType
= true
时,我需要一种获取类型名称的方法。
Type t = typeof(List<String>);
MessageBox.Show( ..?.. );
我想要的是一个弹出消息框,其中包含一个显示“List”内容的代码...我该怎么做?
string GetFriendlyName(Type type)
=> type.IsArray ? $"{GetFriendlyName(type.GetElementType())}[]"
: type.IsGenericType ? $"{type.Name.Remove(type.Name.IndexOf('`'))}<{string.Join(", ", type.GetGenericArguments().Select(GetFriendlyName))}>"
: type.Name;
适用于嵌套的泛型类型和Nullable<T>
数组
//outputs Dictionary<String, List<Nullable<Int32>[]>>
GetFriendlyName(typeof(Dictionary<string, List<int?[]>>));