假设我有以下内容:
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 属性。我有什么办法可以做到这一点吗?
请您参考如下方法:
正如我在评论中所建议的那样,使用协变接口(interface)可以实现这一点。
协变/逆变:http://msdn.microsoft.com/en-us/library/ee207183.aspx
另请参阅:Covariance and 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 );
}
}
}
}
}
}
}
