I need to convert an object to a generic collection, look:
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);
}
}
How can i do this?
Obs: i need with this collection call the except method with another collection (this collection i get with the same way of the currentCollection, with a anotherEntityProperty.GetValue(anotherEntity, null))
var itens = currentCollection.Except(anotherCollection);
Dynamic typing lets you make the compiler and DLR do all the work here:
At execution time, that will do all the reflection work for you and pick the most appropriate type argument.