//1、遍历枚举
foreach (NameEnum item in Enum.GetValues(typeof(NameEnum)))
{
string name= item.ToString();
}
//2、遍历类
public class userInfo
{
public string customerName { get; set; }
public string sex { get; set; }
public string age { get; set; }
public string time { get; set; }
public string quality { get; set; }
public string siteName { get; set; }
}
userInfo user = new userInfo();
System.Reflection.PropertyInfo[] properties =
user.GetType().GetProperties(System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.Public);
if (properties.Length <= 0)
{
throw new Exception("光谱透过率类字段长度为空");
}
foreach (System.Reflection.PropertyInfo item in properties)
{
string name = item.Name;
item.SetValue(user, "111", null);
}
//3、遍历类获取属性值
System.Reflection.PropertyInfo[] properties = BaseClass.Datas.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
if (properties.Length <= 0)
{
throw new Exception("类属性长度为零");
}
foreach (System.Reflection.PropertyInfo item in properties)
{
if (item.Name.Contains("State")) continue;
var value = BaseClass.Datas.GetType().GetProperty(item.Name).GetValue(BaseClass.Datas, null).ToString();
if(value == "名称")
{
var name = item.Name.Substring(5, item.Name.Length - 5);
}
}
C# 遍历枚举和遍历类
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 在本文,你将学会如何遍历并获取枚举的每一项,是一个非常实用的编程思想。文章末尾还备注有其他关于枚举的小技巧哦 使用...
- 开发闲暇时间,把开发过程经常用到的一些代码段做个记录,下面代码段是关于C#中对于枚举(Enum)类型的遍历方法的代...
- Swift 枚举和 C++枚举大不相同. 枚举的功能都被大大扩增, 其中 Swift 的枚举功能最为强大 —— 可...
- 将写内容过程常用的一些内容段做个备份,下面内容段是关于C#分别用前序遍历、中序遍历和后序遍历打印二叉树的内容。 p...