如何遍历静态类中的属性的值?

gerrytang 2012-03-28 11:16:16
代码如下
class con
{
public static constanttype cons
{
get
{
return new constanttype
{
a = new constants { name = "ty", value = 1 },
b = new constants { name = "zy", value = 2 }
};
}

}
}


class constants
{
public string name { get; set; }

public int value { get; set; }
}

class constanttype
{
public constants a { get; set; }

public constants b { get; set; }
}


我想问问如何在遍历出所有的constanttype的某个属性 例如遍历出所有con.cons下的元素的所有name 应该怎么写?谢谢
...全文
513 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
gerrytang 2012-03-28
  • 打赏
  • 举报
回复
我已经写了如下代码 调试的时候发现
Type t = con.consts.GetType ();
foreach (var ex in t.GetProperties())
{
object ab = ex.GetValue(con .consts ,null);
}


ab里面确实是一个包含name和value的属性,那么如何将ab里面的属性的单个值分离出来呢?比如我只要name呢?我网上看了下 很多是通过type来分离,那假如我有2个同样的type呢?
heihei1018 2012-03-28
  • 打赏
  • 举报
回复
1L正解~
  • 打赏
  • 举报
回复
++++
到处你都是沙发!![Quote=引用 1 楼 的回复:]

typeof(constanttype).GetProperties,他还有个重载可以指定属性的分类,看帮助吧
[/Quote]
SQL777 2012-03-28
  • 打赏
  • 举报
回复
Type
bdmh 2012-03-28
  • 打赏
  • 举报
回复
typeof(constanttype).GetProperties,他还有个重载可以指定属性的分类,看帮助吧

111,126

社区成员

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

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

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