我正在进行代码生成工作,并在泛型方面遇到了问题。以下是导致问题的“简化”版本。
Dictionary<string, DateTime> dictionary = new Dictionary<string, DateTime>();
string text = dictionary.GetType().FullName;
使用上面的代码片段,text
的值如下:
System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.DateTime, mscorlib,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
有没有一种方法可以在不解析上述字符串的情况下以不同的格式获取类型名(type
)?我希望text
的结果如下:
System.Collections.Generic.Dictionary<System.String, System.DateTime>
.FullName
并改用.ToString()
,则会得到更易读且接近所需的 "text"System.Collections.Generic.Dictionary`2[System.String,System.DateTime]
。 - Jeppe Stig Nielsen