如何检查我接收的对象是否为方法结果不是ValueType
而不是IEnumerable<ValueType>
?检查IEnumerable是否为ValueType(运行时)
这里是我写的:
MethodInfo selectedOverload = SelectOverload(selectedMethodOverloads);
object result = ExecuteAndShowResult(selectedOverload);
ExploreResult(result);
private static void ExploreResult(object result)
{
if (result != null &&
!(result is ValueType) &&
!((IEnumerable)result).GetType().GetProperty("Item").PropertyType) is ValueType)
)
Console.WriteLine("explore");
}
不幸的PropertyType
类型为Type
,它的内容是我需要检查的类型(例如int
),但我不知道怎么样。
编辑:
确定,.IsValueType
工作,但现在我想也排除字符串(这不被视为值类型),还等什么?
!(((IEnumerable)result).GetType().GetProperty("Item").PropertyType is string)
不起作用!
编辑2:
只是简单地回答自己:
:!(((IEnumerable)result).GetType().GetProperty("Item").PropertyType == typeof(string))
问题仍然是开放的,如果我想从一个基类检查继承什么
!(((IEnumerable)result).GetType().GetProperty("Item").PropertyType == typeof(BaseClass))
不起作用,因为的typeof检查运行时类型,如果PropertyType == InheritedClassType
它将返回false ...
你仅仅意味着'的getProperty( “Item”)。PropertyType.IsValueType'? –
Type.IsValueType? –
感谢您的回复!这是我所需要的。 – Teejay