泛型问题。判断是否是Dictionary或者List

mo1shang 2014-07-14 04:14:14
我定义了一个Dictionary<string,string> s的属性。
然后我s.getType();获得它的Type对象。
怎么判断他是Dictionary呢? 假设我不知道Key的类型和Value的类型
...全文
1181 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
卧_槽 2014-07-18
  • 打赏
  • 举报
回复
你似乎应该判断是不是Dictionary<string,string> ,而不是Dictionary。这两者不是一码事。
  • 打赏
  • 举报
回复
引用 12 楼 diaodiaop 的回复:
引用
我定义了一个Dictionary<string,string> s的属性。 然后我s.getType();获得它的Type对象。 怎么判断他是Dictionary呢?
是我语文不好吗? 这还用判断 你自己不知道啊...... 好比你定义了一个int 你还要判断他是不是int吗......
LZ大概是在一个方法中判断传入的参数吧。。但总感觉这个思路不太好,你真正要解决的不是“判断”,而是尽量“避免”这种判断。
by_封爱 版主 2014-07-18
  • 打赏
  • 举报
回复
引用
我定义了一个Dictionary<string,string> s的属性。 然后我s.getType();获得它的Type对象。 怎么判断他是Dictionary呢?
是我语文不好吗? 这还用判断 你自己不知道啊...... 好比你定义了一个int 你还要判断他是不是int吗......
mian_2661997757 2014-07-18
  • 打赏
  • 举报
回复
用泛型存储不同数据表 class Types<T> { public T Num; //声明编号字段 public T Name; //声明姓名字段 public T Sex; //声明性别字段 public T Age; //声明年龄字段 public T Birthday; //声明生日字段 public T Salary; //声明薪水字段 } private void button1_Click(object sender, EventArgs e) { Types<object> Exte = new Types<object>();//实例化泛型类对象 //为泛型类中声明的字段进行赋值,存储不同类型的值 Exte.Num = 1; Exte.Name = "王老师"; Exte.Sex = "男"; Exte.Age = 25; Exte.Birthday = Convert.ToDateTime("1986-06-08"); Exte.Salary = 1500.45F;//CodeGo.net/ //将泛型类中各字段的值显示在文本框中 textBox1.Text = Exte.Num.ToString(); textBox2.Text = Exte.Name.ToString(); textBox3.Text = Exte.Sex.ToString(); textBox4.Text = Exte.Age.ToString(); textBox5.Text = Exte.Birthday.ToString(); textBox6.Text = Exte.Salary.ToString(); }
youzelin 2014-07-18
  • 打赏
  • 举报
回复
#9 都已经给出正确答案了,楼主你就用 9 楼的。

Dictionary<string, int> dic = new Dictionary<string, int>();
if (dic.GetType().IsGenericType && dic.GetType().GetGenericTypeDefinition() == typeof(Dictionary<,>))
{
    ......
}
会飞的溜溜 2014-07-18
  • 打赏
  • 举报
回复
public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback
会飞的溜溜 2014-07-18
  • 打赏
  • 举报
回复
所谓的类型就是一个对象,对象就是一个类型,面向对象编程:一切都是对象,都可以看做类型有封装的基本类型和你写的类型(其实就是你写的类)
会飞的溜溜 2014-07-18
  • 打赏
  • 举报
回复
string ss = ""; ss = ss.GetType().Name; Dictionary<string,string> gh=new Dictionary<string,string> (); if (gh.GetType().Name == typeof(Dictionary<,>).Name) { ss = gh.GetType().Name; } 你这是基本对象概念没明白,你看看它是什么类型.GetType()得到就是一个对象就是一个类型
  • 打赏
  • 举报
回复
.Name.Contains("Dictionary") 这是不靠谱的。你稍微懂点心思,就能随便写出一个bug测试。
  • 打赏
  • 举报
回复
var x = s.GetType();
var t = typeof(Dictionary<,>);
var test = x.IsGenericType && x.GetGenericTypeDefinition() == t;
threenewbee 2014-07-16
  • 打赏
  • 举报
回复
Dictionary<string, string>不是Dictionary,两者没有任何关系。当然if(s.getType() is Dictionary)这个是报错的- -。 你应该用它和Type.MakeGenericType()得到的Dictionary<string, string>去比较。
smthgdin_020 2014-07-16
  • 打赏
  • 举报
回复
引用 4 楼 dongxinxi 的回复:
if (s is IDictionary)
{
}
或
if (typeof(IDictionary).IsAssignableFrom(s.GetType()))
{
}
+1
gwhzh 2014-07-16
  • 打赏
  • 举报
回复
s.GetType() == typeof(Dictionary)
forcyever 2014-07-14
  • 打赏
  • 举报
回复
引用 3 楼 duanzi_peng 的回复:
s.GetType().Name.Contains("Dictionary")
UP
  • 打赏
  • 举报
回复
if (s is IDictionary)
{
}
或
if (typeof(IDictionary).IsAssignableFrom(s.GetType()))
{
}
exception92 2014-07-14
  • 打赏
  • 举报
回复
s.GetType().Name.Contains("Dictionary")
mo1shang 2014-07-14
  • 打赏
  • 举报
回复
if(s.getType() is Dictionary)这个是报错的- -。
於黾 2014-07-14
  • 打赏
  • 举报
回复
if(s.getType() is Dictionary)

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧