繁体   English   中英

创建具有 class 属性的列表

[英]Create a list with class properties

我的 class 有一些具有基本类型和字典的属性:

public class Statistic
{
    public int Id { get; set; }
    public object Parent { get; set; }
    public bool IsExpanded { get; set; }
    public string Data { get; set; }
    public string Code { get; set; }
    public Dictionary<string, object> DataDictionary { get; set; } = new Dictionary<string, object>();

    public Statistic()
    {
    }
}

现在我想创建一个包含所有属性名称的列表,我这样做了:

                List<string> columns = new List<string>();

                PropertyInfo[] properties = typeof(Statistic).GetProperties();
                foreach (PropertyInfo property in properties)
                {
                    columns.Add(property.Name);
                    if (property.Name=="DataDictionary")
                    {
// How to iterate thru my Dictionary?
                    }
                }

如何遍历我的字典?

您可以直接使用DataDictionary并像这样迭代。

foreach(KeyValuePair<string, object> kvp in DataDictionary)
{
    // use the kvp.Value and kvp.Key
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM