Use Type.IsValueType
:
private static void ExploreResult(object result)
{
if (result != null &&
!(result.GetType().IsValueType) &&
!((IEnumerable)result).GetType().GetProperty("Item").PropertyType.IsValueType)
)
Console.WriteLine("explore");
}
Although if result
is not a value type but not an IEnumerable
than you'll get a cast error. That check needs some work.
Answer to second part
!((IEnumerable)result).GetType().GetProperty("Item").PropertyType is string)
is always false because PropertyType
returns a Type
which is never a string. I think you want
!(result.GetType().GetProperty("Item").PropertyType == typeof(string))
Note that I took out the cast to IEnumerable
since you're looking for a property through reflection anyways, so the cast is irrelevant.
Answer to third edit
I want to check the inheritance from a baseclass
For that you want type.IsAssignableFrom()
:
Type itemType = result.GetType().GetProperty("Item").PropertyType;
bool isInheritedFromBaseClass =
typeof(BaseClass).IsAssignableFrom(itemType);