我需要将对象转换为泛型集合,请看:如何投射泛型集合中的对象?
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);
}
}
我怎么能这样做呢?
观测值:我需要与此集合调用方法,除了与另一个集合(这个集合我与currentCollection以同样的方式获得,具有anotherEntityProperty.GetValue(anotherEntity, null)
)
var itens = currentCollection.Except(anotherCollection);
之后你会怎么做?如果您可以更多地了解更广泛的背景,我们可以提供一种替代方法。 – 2012-03-01 17:03:41
好的!编辑的问题。 – 2012-03-01 17:07:48
你不知道'anotherCollection'的类型吗?你从哪里得到?你能否早些时候用反射进行单一的通用呼叫?如果您使用的是C#4,动态类型也可以使这更简单。 – 2012-03-01 17:09:15