gpt4 book ai didi

c# - 如何在我的类中实现 Foreach 以便我可以获得每个键名和值?

转载 作者:太空宇宙 更新时间:2023-11-03 21:23:02 24 4
gpt4 key购买 nike

public class Zone
{
public string zoneID { get; set; }
public string zoneName { get; set; }
public string zonePID { get; set; }
}

我想为 Zone 使用 foreach,比如

var zone = new Zone(){zoneId = "001", zoneName = "test"};
foreach(var field in zone)
{
string filedName = field.Key; //for example : "zoneId"
string filedValue = filed.value; //for example : "001"
}

我只是不知道如何在 Zone 类中实现 GetEnumerator()

最佳答案

你不能枚举一个类的属性(以一种简单的方式)

在类中使用字符串数组或字符串列表或字典。

注意:确实可以使用反射枚举类的属性,但这不是您的情况。

关于c# - 如何在我的类中实现 Foreach 以便我可以获得每个键名和值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29339743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com