遍历未知对象

时间:2019-03-27 10:26:03

标签: c#

我面临的问题是遍历object

我的Resource类的一部分看起来像这样:

public List<PhoneCodeInfo> PhoneCodes { get; set; }
public List<CountryInfo> Countries { get; set; }
public List<BrandDetail> Brands { get; set; }
public List<FileTypeDetail> FileTypes { get; set; }
public object Enums { get; set; } // <- is the problem

我可以轻松遍历除Enums之外的所有内容,因为“我”不知道它是什么样。

enter image description here

我像这样遍历PhoneCodes

foreach (var phoneCode in value.PhoneCodes)
{
    current += GetHashString(phoneCode.Code);
}

但是我该如何循环Enums或获取其子项。

我尝试将Enums转换为Json对象,但是没有运气

1 个答案:

答案 0 :(得分:1)

尝试以下操作:

        public enum Test
        {
            One,
            Two,
            Three,
            Four
        }
        static void Main(string[] args)
        {

            string[] names = Enum.GetNames(typeof(Test)).ToArray();
            foreach (var name in names)
            {
                int value = (int)Enum.Parse(typeof(Test),name);
            }
        }