在将反射返回的泛型EntitySet转换为具有BaseClass类型的EntitySet时,我遇到了一个问题。
我的所有Linq2Sql类都继承自名为LinqClassBase的基类,如下所示:
public partial class MyTable1 : LinqClassBase我正在这个基类中编写一个方法,它需要遍历所有子EntitySets。
我可以像matchingEntitySetProperty一样检索PropertyInfo OK。
// The GetMatchingProperty method (not shown)
// simply gets all the properties with Type EntitySet
var matchingProperty = entitySetProperty.GetMatchingProperty(this.GetType());我也可以得到它的值。
// This returns EntitySet<MyTable1>
var matchingSet = matchingProperty.GetValue(this);这里的问题是我不能调用ToList方法,因为对象不是强类型的。
var newList = matchingSet.ToList().ConvertAll(
x => x.MyLinqBaseClassMethod()
);我尝试将其转换为EntitySet,但它返回null:
// This returns null
var matchingSet = matchingProperty.GetValue(this) as EntitySet<LinqClassBase>;为什么此强制转换返回null?我猜是因为C#不能将EntitySet转换为EntitySet。
如果这个造型是不可能的,有没有其他的方法呢?
发布于 2013-11-23 01:02:47
我想通了。将其转换为IEnumerable使我可以访问ToList方法,而不会导致它返回null。
var matchingSet = matchingProperty.GetValue(this) as IEnumerable<LinqClassBase>;我仍然不知道为什么强制转换它有EntitySet失败。但至少我有我的解决方案。
https://stackoverflow.com/questions/20149732
复制相似问题