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()得到就是一个对象就是一个类型
public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback
#9 都已经给出正确答案了,楼主你就用 9 楼的。Dictionary<string, int> dic = new Dictionary<string, int>(); if (dic.GetType().IsGenericType && dic.GetType().GetGenericTypeDefinition() == typeof(Dictionary<,>)) { ...... }
{
}
或
if (typeof(IDictionary).IsAssignableFrom(s.GetType()))
{
}
var t = typeof(Dictionary<,>);
var test = x.IsGenericType && x.GetGenericTypeDefinition() == t;
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();
}
是我语文不好吗?这还用判断 你自己不知道啊......好比你定义了一个int 你还要判断他是不是int吗......
是我语文不好吗?这还用判断 你自己不知道啊......好比你定义了一个int 你还要判断他是不是int吗......
LZ大概是在一个方法中判断传入的参数吧。。但总感觉这个思路不太好,你真正要解决的不是“判断”,而是尽量“避免”这种判断。
ss = ss.GetType().Name;
Dictionary<string,string> gh=new Dictionary<string,string> ();
if (gh.GetType().Name == typeof(Dictionary<,>).Name)
{
ss = gh.GetType().Name;
}
你这是基本对象概念没明白,你看看它是什么类型.GetType()得到就是一个对象就是一个类型
if (dic.GetType().IsGenericType && dic.GetType().GetGenericTypeDefinition() == typeof(Dictionary<,>))
{
......
}