如何检索存储在对象类中的列表

问题描述 投票:-2回答:1
public class A
{
    public long TestCaseId { get; set; }
    public byte TestCaseNumber { get; set; }

    public object FetchList { get; set; }
} 
class Program
{
    static void Main(string[] args)
    {
        List<int> a = new List<int>(){ 1, 2, 3, 4 };
        List<A> Objectlst = new List<A>();
        Objectlst .Add(new A { TestCaseId = 1, TestCaseNumber = 1, FetchList = a });
        foreach (var item in Objectlst)
        {
            Console.WriteLine(item.FetchList);
        }
    }
}

[类别为A。如果我要为对象数据类型FetchList分配通用列表,有人可以告诉我如何通过此属性FetchList检索存储在列表中的所有值吗?

c# list object console
1个回答
0
投票
foreach (var item in Objectlst)
{
    var list = item.FetchList as List<int>;
    if (list != null)
        foreach (int num in list)
            Console.WriteLine(num);
}
© www.soinside.com 2019 - 2024. All rights reserved.