代码之家  ›  专栏  ›  技术社区  ›  TrashMachine139

为什么当对象类型为IList时,我得到的“foreach语句无法对”object“类型的变量进行操作

  •  0
  • TrashMachine139  · 技术社区  · 2 年前

    namespace NS
    {
        using System;
        using System.Collections;
        using System.Collections.Generic;
        using System.Linq;
        
        public class Accelerator 
        {
            public string AccName { get; set; }    
        }
        public class DeviceInfo
        {
            public int Ram { get; set; }
            public List<Accelerator> Accelerator { get; set; }
        }
        
        public class SolutionPods
        {
            public DeviceInfo DeviceInfo { get; set; }
        }
        
        public class Solution
        {
            public List<SolutionPods> SolutionPods { get; set; }
        }
    
        public class HelloWorld
        {
            public static void Main(string[] args)
            {
                var acc1 = new Accelerator { AccName = "accelerator Name" };
                List<Accelerator> accList1 = new List<Accelerator>();
                accList1.Add(acc1);
                
                var devInfo1 = new DeviceInfo { Ram = 64, Accelerator = accList1 };
                
                var solComponet = new SolutionPods { DeviceInfo = devInfo1 };
                List<SolutionPods> SCList = new List<SolutionPods>();
                SCList.Add(solComponet);
                var solution = new Solution { SolutionPods = SCList };
                
    
                foreach (var solutionComponet in solution.SolutionPods)
                {
                    var solutionInformation = solutionComponet.DeviceInfo;
                    foreach (var solutionProperty in solutionInformation.GetType().GetProperties())
                    {
                        var solutionValue = solutionProperty.GetValue(solutionInformation);
                        if (solutionValue is IList)
                        {
                            Console.WriteLine("IList found");
                            
                            // System.Collections.Generic.List`1[NS.Accelerator]
                            Console.WriteLine(solutionValue);
                            
                            /*
                            /tmp/AFBHdRy49g.cs(64,43): error CS1579: foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public instance definition for 'GetEnumerator'
                            */
                            foreach (var s in solutionValue)
                            {
                                Console.WriteLine(s);
                            }
                        }
                    }
                }
                Console.WriteLine("Setup Complete");
            }
        }
    }
    

    我不是真的抓到了一张名单吗?或者我需要将对象转换为不同的形式,使其具有GetEnumerator属性吗?

    2 回复  |  直到 2 年前
        1
  •  3
  •   StriplingWarrior    2 年前

    在C#中,一旦声明了变量,它就会保持其声明的类型。就你而言 var solutionValue 使用推断的类型 object Property.GetValue() 返回。

    当遇到以下条件时,编译器不会实现类型收缩:

    if (solutionValue is IList)
    

    if (solutionValue is IList solutions)
    {
       ...
       foreach (var s in solutions)
    
        2
  •  2
  •   Ondrej Tucny    2 年前

    编译器执行 类型检查所提供的值是否(静态)为 IEnumerable . 一 object 不是。A. (IList)solutionValue 是的。

    if (solutionValue is IList list) // joining a type test and a cast
    {
        foreach (var s in list) // here list is an IList
        {
            Console.WriteLine(s);
        }
    }