我需要将对象转换为通用集合,看:
var currentEntityProperties = currentEntity.GetType().GetProperties();
foreach (var currentEntityProperty in currentEntityProperties)
{
if (currentEntityProperty.PropertyType.GetInterfaces().Any(
x => x.IsGenericType &&
x.GetGenericTypeDefinition() == typeof(ICollection<>)))
{
var collectionType = currentEntityProperty.PropertyType.GetInterfaces().Where(
x => x.IsGenericType &&
x.GetGenericTypeDefinition() == typeof(ICollection<>)).First();
var argumentType = collectionType.GetGenericArguments()[0];
// now i need to convert the currentEntityProperty into a collection, something like that (this is wrong, so, what is thr right way?):
var currentCollection = (ICollection<argumentType.GetType()>)currentEntityProperty.GetValue(currentEntity, null);
}
}
我该怎么做?
Obs:我需要用这个集合调用另一个集合的 except 方法(我用与 currentCollection 相同的方式获得这个集合,使用 anotherEntityProperty.GetValue(anotherEntity, null)
)
var itens = currentCollection.Except(anotherCollection);
动态类型让您可以让编译器和 DLR 在这里完成所有工作:
dynamic currentCollection = ...;
dynamic anotherCollection = ...;
dynamic items = Enumerable.Except(currentCollection, anotherCollection);
在执行时,这将为您完成所有反射工作并选择最合适的类型参数。
我是一名优秀的程序员,十分优秀!