45
Simple. The easiest thing to do is the following:
IList<T> listTest = null;
try{
listTest = ((IList<T>)yourObject);
}
catch(Exception listException)
{
//your object doesn't support IList and is not of type List<T>
}
IEnumerable<T> enumerableTest = null;
try{
enumerableTest = ((IEnumerable<T>)yourObject);
}
catch(Exception enumerableException)
{
//your object doesn't suport IEnumerable<T>;
}
==================================================
You can also try this which doesn't involve multiple try/catch blocks. It's better if you can avoid using those because each condition is actually evaluated by the runtime during runtime... it's bad code (though sometimes there's no way around it).
Type t = yourObject.GetType();
if( t is typeof(List<OjbectType>) ) //object type is string, decimal, whatever...
{
// t is of type List<ObjectType>...
}
else if( t is typeof(IEnumerable<ObjectType>)
{
// t is of type IEnumerable<ObjectType>...
}
else
{
// t is some other type.
// use reflection to find it.
}