【发布时间】:2008-12-30 22:13:42
【问题描述】:
我正在处理代码生成,但遇到了泛型问题。这是导致我出现问题的“简化”版本。
Dictionary<string, DateTime> dictionary = new Dictionary<string, DateTime>();
string text = dictionary.GetType().FullName;
使用上面的代码sn -p,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(),您将获得更易读的“文本”System.Collections.Generic.Dictionary`2[System.String,System.DateTime],并且更接近您想要的。
标签: c# generics reflection