C# 无法强制转换EntityCollection类型的对象<;派生>;到EntityCollection<;基地>;
我正在使用反射从EF4域实体获取C# 无法强制转换EntityCollection类型的对象<;派生>;到EntityCollection<;基地>;,c#,entity-framework,reflection,casting,polymorphism,C#,Entity Framework,Reflection,Casting,Polymorphism,我正在使用反射从EF4域实体获取EntityCollection属性。一个示例实体可能有许多集合,其中包含具有公共基的类型GetValue()返回一个对象,但我需要将其转换为EntityCollection,甚至只是IEnumerable。但是怎么做呢?(哎呀,从C#4开始,强制转换为IEnumerable确实有效) 示例模型 public class Derived : Base { ... } public class AnotherDerived : Base { ... } public
EntityCollection
属性。一个示例实体可能有许多集合,其中包含具有公共基的类型GetValue()
返回一个对象
,但我需要将其转换为EntityCollection
,甚至只是IEnumerable
。但是怎么做呢?(哎呀,从C#4开始,强制转换为IEnumerable确实有效)
示例模型
public class Derived : Base { ... }
public class AnotherDerived : Base { ... }
public class Example : Base
{
public virtual ICollection<Derived> Items { get; set; }
public virtual ICollection<AnotherDerived> OtherItems { get; set; }
}
public static List<T> GetCollectedEntities<T>(this BaseEntity entity)
where T : BaseEntity
{
var result = new List<T>();
foreach (var c in GetCollections<T>(entity))
foreach (var item in (EntityCollection<T>)c) //ERROR
result.Add(item);
return result;
}
public static List<object> GetCollections<T>(this BaseEntity entity)
where T : BaseEntity
{
var collections = new List<object>();
var props = from p in entity.GetType().GetProperties()
let t = p.PropertyType
where t.IsGenericType
&& t.GetGenericTypeDefinition() == typeof(ICollection<>)
let a = t.GetGenericArguments().Single()
where a == typeof(T) || a.IsSubclassOf(typeof(T))
select p;
foreach (var p in props)
collections.Add(p.GetValue(entity, null));
return collections;
}
Unable to cast object of type
'System.Data.Objects.DataClasses.EntityCollection`1[HTS.Data.ServiceOrder]'
to type
'System.Data.Objects.DataClasses.EntityCollection`1[HTS.Data.IncomingServiceOrderBase]'.
真实世界错误
public class Derived : Base { ... }
public class AnotherDerived : Base { ... }
public class Example : Base
{
public virtual ICollection<Derived> Items { get; set; }
public virtual ICollection<AnotherDerived> OtherItems { get; set; }
}
public static List<T> GetCollectedEntities<T>(this BaseEntity entity)
where T : BaseEntity
{
var result = new List<T>();
foreach (var c in GetCollections<T>(entity))
foreach (var item in (EntityCollection<T>)c) //ERROR
result.Add(item);
return result;
}
public static List<object> GetCollections<T>(this BaseEntity entity)
where T : BaseEntity
{
var collections = new List<object>();
var props = from p in entity.GetType().GetProperties()
let t = p.PropertyType
where t.IsGenericType
&& t.GetGenericTypeDefinition() == typeof(ICollection<>)
let a = t.GetGenericArguments().Single()
where a == typeof(T) || a.IsSubclassOf(typeof(T))
select p;
foreach (var p in props)
collections.Add(p.GetValue(entity, null));
return collections;
}
Unable to cast object of type
'System.Data.Objects.DataClasses.EntityCollection`1[HTS.Data.ServiceOrder]'
to type
'System.Data.Objects.DataClasses.EntityCollection`1[HTS.Data.IncomingServiceOrderBase]'.
这似乎是你应该能做的事情,不是吗?但这是不允许的,原因如下
EntityCollection
是可写的,因此如果将EntityCollection
强制转换为EntityCollection
,则可以将基本对象插入到集合中。这意味着您现在在EntityCollection
中有一个类的实例,该类不是派生的,也不是派生的子类。然后呢?EntityCollection
上的迭代器需要一个派生的迭代器,它将以各种令人兴奋的方式失败。那么转换为IEnumerable
怎么样?哈哈,我以为我已经尝试过了,但出现了一个错误。它起作用了。谢谢。如果你在C#4中,你可以对IEnumerable进行协变赋值。但是,它在早期版本的.net中不起作用。看见