1

说我有以下内容:

public class MyContainer
{
    public string ContainerName { get; set; }
    public IList<Square> MySquares { get; set; }
    public IList<Circle> MyCircles { get; set; }
    public MyContainer()
    {
        MySquares = new List<Square>();
        MyCircles = new List<Circle>();
    }
}

public class Shape
{
    public int Area { get; set; }
}

public class Square : Shape
{
}

public class Circle : Shape
{
}

现在我有一个这样的功能:

private static void Collect(MyContainer container)
{
    var properties = container.GetType().GetProperties();
    foreach (var property in properties)
    {
        if (property.PropertyType.IsGenericType &&
            property.PropertyType.GetGenericTypeDefinition() == typeof(IList<>) &&
            typeof(Shape).IsAssignableFrom(property.PropertyType.GetGenericArguments()[0]))
        {
            var t = property.GetValue(container, null) as List<Square>;
            if (t != null)
            {
                foreach (Shape shape in t)
                {
                    Console.WriteLine(shape.Area);
                }
            }
        }
    }

当我到达该MySquares物业时,这按我想要的方式工作,但是我希望改用以下方式:

var t = property.GetValue(container, null) as List<Shape>;

我希望它会循环遍历具有类似列表的所有 MyContainer 属性。我有什么办法可以做到这一点?

4

2 回答 2

2

如果将GetValue行更改为以下内容,则可以:

var t = property.GetValue(container, null) as IEnumerable<Shape>;

我使用以下代码对其进行了测试:

var c = new MyContainer();
c.MySquares.Add(new Square() { Area = 5, });
c.MySquares.Add(new Square() { Area = 7, });
c.MySquares.Add(new Square() { Area = 11, });
c.MyCircles.Add(new Circle() { Area = 1, });
c.MyCircles.Add(new Circle() { Area = 2, });
c.MyCircles.Add(new Circle() { Area = 3, });
Collect(c);

并得到了这些结果:

5
7
11
1
2
3
于 2012-08-03T01:21:04.467 回答
2

正如我在评论中建议的那样,使用协变接口可以实现这一点。

协方差/逆变:http: //msdn.microsoft.com/en-us/library/ee207183.aspx
另见:协方差和 IList

工作样本

namespace Covariance
{
    public class MyContainer
    {
        public string ContainerName { get; set; }
        public IList<Square> MySquares { get; set; }
        public IList<Circle> MyCircles { get; set; }
        public MyContainer() {
            MySquares = new List<Square>();
            MyCircles = new List<Circle>();
        }
    }

    public class Shape
    {
        public int Area { get; set; }
    }

    public class Square : Shape
    {
    }

    public class Circle : Shape
    {
    }   

    class Program
    {
        static void Main( string[] args ) {

            MyContainer mc = new MyContainer();
            mc.MyCircles.Add( new Circle { Area = 60 } );

            Collect( mc );
            Console.ReadLine();
        }

        private static void Collect( MyContainer container ) {
            var properties = container.GetType().GetProperties();
            foreach( var property in properties ) {
                if( property.PropertyType.IsGenericType &&
                    property.PropertyType.GetGenericTypeDefinition() == typeof( IList<> ) &&
                    typeof( Shape ).IsAssignableFrom( property.PropertyType.GetGenericArguments()[0] ) ) {
                    var t = property.GetValue( container, null ) as IEnumerable<Shape>;
                    if( t != null ) {
                        foreach( Shape shape in t ) {
                            Console.WriteLine( shape.Area );
                        }
                    }
                }
            }
        }
    }
}
于 2012-08-03T01:23:12.000 回答