Casting to a generic base type

寵の児 提交于 2019-12-13 04:27:38

问题


I have the following class structure:

public class RelationBase : Entity
{
}

public class RelationURL : RelationBase
{
}

public class RelationBaseList<T> where T: RelationBase
{
  public List<T> Collection { get; set; }
}

public class RelationURLList : RelationBaseList<RelationURL>
{
}

public class RefTest
{
  public RelationURLList urlList { get; set; }

  public RefTest()
  {
    urlList = new RelationURList();
    urlList.Collection = new List<RelationUR>();
    urlList.Collection.Add(new RelationUR());
  }
}

Via reflection, I get an instance of RelationURLList and I want to cast it to RelationBaseList<RelationBase>. Unfortunately, I can only cast it to RelationBaseList<RelationURL>

RefTest obj = new RefTest();
PropertyInfo[] props = obj.GetType().GetProperties();
foreach (PropertyInfo prop in props)
{
  object PropertyValue = prop.GetValue(obj, null);

  object cast1 = PropertyValue as RelationBaseList<RelationURL>;
  object cast2 = PropertyValue as RelationBaseList<RelationBase>;
}

In cast1, I have the expected object, but cast2 is null. As I don't want to cast to each possibly derived class from RelationBase, I want to use the second cast (cast2). Any idea, how I can get the object, without casting to each single derived type?


回答1:


What do you want to do with the RelationBaseList.Collection?

Using an interface could be a possible solution, if you only want to access the values of the Collection rather than setting them:

public interface IRelationBaseList
{
    IEnumerable<RelationBase> Collection { get; }
}

public class RelationBaseList<T> : IRelationBaseList where T : RelationBase
{
    IEnumerable<RelationBase> IRelationBaseList.Collection
    {
        get { return Collection; }
    }
    public List<T> Collection { get; set; }
}

public class RelationURLList : RelationBaseList<RelationURL>
{

}

So you can do:

RefTest obj = new RefTest();
PropertyInfo[] props = obj.GetType().GetProperties();
foreach (PropertyInfo prop in props)
{
    object PropertyValue = prop.GetValue(obj, null);

    var relationBaseList = PropertyValue as IRelationBaseList;
    foreach (var relationBase in relationBaseList.Collection)
    { 
        // do something with it
    }
}



回答2:


You can't.

The compiler cannot verify that what you're doing is legal.

If RelationBaseList was an interface, you could tag the generic parameter with the out keyword, to signal that the interface only allows retrieval of data from the object.

However, the way your code is organized, the compiler cannot verify that you're not trying to do this:

case2.Add(new SomeOtherRelationObject());

and the whole point of generics is to make type-safe code.

So no, you cannot do that.



来源:https://stackoverflow.com/questions/20632568/casting-to-a-generic-base-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!