简体   繁体   English

如何在课堂上实现Foreach,以便获得每个键的名称和值?

[英]How to implement Foreach in my class so I can get each key name and value?

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

I want to use foreach for Zone, like 我想将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"
}

I just don't konw how to implement GetEnumerator() in Zone class 我只是不知道如何在Zone类中实现GetEnumerator()

You can't enumerate the properties of a class (in a simple way) 您无法枚举类的属性 (以简单的方式)

Use a string array or string list or a dictionary inside your class. 在类中使用字符串数组或字符串列表或字典。

Note: Indeed it is possible to enumerate the properties of a class using Reflection, but this is not the way to go in your case. 注意:确实可以使用Reflection枚举类的属性,但这不是您要解决的方法。

foreach(var field in zone)
{
   string filedName = field.zoneID;  //Id of property from Zone Class
   string filedValue = filed.zoneName ; //name of property from Zone Class
}

You could equip Zone with this method: 您可以使用以下方法为Zone配备:

public Dictionary<string, string> AsDictionary()
{
  return new Dictionary<string, string>
    {
      { "zoneID", zoneID },
      { "zoneName", zoneName },
      { "zonePid", zonePid },
    };
 }

Then you can foreach that. 然后你就可以foreach说。

Alternatively, you can implement GetEnumerator() as an iterator block where you yield return the three new KeyValuePair<string, string> . 或者,您可以将GetEnumerator()实现为迭代器块,并在其中yield return三个new KeyValuePair<string, string>

I am not saying that this design is particularly recommendable. 我并不是说这种设计特别值得推荐。

Thanks eveyrone! 谢谢eveyrone! It Seems I need to use reflection to achieve the goal. 看来我需要使用反射来达成目标。

System.Reflection.PropertyInfo[] pis = zone.GetType().GetProperties();
foreach (var prop in pis)
{
    if (prop.PropertyType.Equals(typeof(string))) 
    {
        string key = prop.Name;
        string value = (string)prop.GetValue(zome, null);
        dict.Add(key, value); //the type of dict is Dictionary<str,str>
    }
}

Just don't know wheather this is a good solution. 只是不知道惠特尔,这是一个很好的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过类名称获取MEF导出值? - How can I get a MEF exported value by a class name? 如何添加到这样的类中,以便进行foreach循环? - How can I add to a class like this so I can foreach loop? 我的程序有两个接口,并且都有一个同名的方法。 那么,我们如何在继承两个接口的子类中实现它们呢? - My program has two interfaces and both have a method with the same name. So, how can we implement them in a child class inheriting both interfaces? 如何实现其值也是键的字典? - How can I implement a dictionary whose value is also a key? 我怎样才能克服以下错误: System.IO.IOException: &#39;目录名无效。 在我的 foreach 循环中? - How can I get past the following error: System.IO.IOException: 'The directory name is invalid. in my foreach loop? 如何在foreach循环中获取原始/父对象的值? - How can I Get an original/parent object value in foreach loop? 如何从密钥名称(动态对象)中包含数字的json属性获取值? - How can I get value from json property that has numbers in key name(dynamic object)? 如何根据每个类的值订购一个类列表? - How can I order a list of class based on a value of each class? 如何在 [Setup] 或 [OneTimeSetup] 中传递参数,以便我不必在测试类中调用该方法? - How can I pass parameters in a [Setup] or [OneTimeSetup] so I so not have call the method in my test class? 我如何为我的类实现IDisposable,以便它可以在&#39;using&#39;块中使用? - How would I implement IDisposable for my classes so that it can be used in a 'using' block?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM