【c#】遍历获得一个类的所有属性名

 

Type t = tc.GetType();//获得该类的Type
foreach (PropertyInfo pi in t.GetProperties)
{
    object value1 = pi.GetValue(tc, null));//用pi.GetValue获得值
    string name = pi.Name;//获得属性的名字
    //获得属性的类型
   if(value1.GetType() == typeof(int))
   {
       //进行你想要的操作
   }
}

注意:必须要设置了get 和set方法的属性,反射才能获得该属性
public int Pid
{

  get { return pid; }

  set { pid = value; }
}

https://blog.csdn.net/mao_mao37/article/details/85265295

posted @ 2021-03-12 14:34  不溯流光  阅读(369)  评论(0编辑  收藏  举报