我需要將對象轉換爲泛型集合,請看:如何投射泛型集合中的對象?
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