C# 检查IEnumerable是否为ValueType(在运行时)
如何检查作为方法结果收到的对象是否不是C# 检查IEnumerable是否为ValueType(在运行时),c#,reflection,value-type,C#,Reflection,Value Type,如何检查作为方法结果收到的对象是否不是ValueType且不是IEnumerable 以下是我写的: MethodInfo selectedOverload = SelectOverload(selectedMethodOverloads); object result = ExecuteAndShowResult(selectedOverload); ExploreResult(result); private static void ExploreResult(object result)
ValueType
且不是IEnumerable
以下是我写的:
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
起作用了,但是现在我还想排除字符串(不能识别为valuetype),那又怎样
!(((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…使用:
尽管如果result
不是值类型,但不是IEnumerable
,您将得到一个强制转换错误。那张支票需要修改一下
对第二部分的回答
!((IEnumerable)result).GetType().GetProperty("Item").PropertyType is string)
始终为false,因为PropertyType
返回的类型从来不是字符串。我想你想要
!(result.GetType().GetProperty("Item").PropertyType == typeof(string))
请注意,我将强制转换为IEnumerable
,因为您正在通过反射查找属性,所以强制转换与此无关
回答第三次编辑
!((IEnumerable)result).GetType().GetProperty("Item").PropertyType is string)
我想检查基类的继承
对于您想要的类型。IsAssignableFrom()
:
使用:
尽管如果result
不是值类型,但不是IEnumerable
,您将得到一个强制转换错误。那张支票需要修改一下
对第二部分的回答
!((IEnumerable)result).GetType().GetProperty("Item").PropertyType is string)
始终为false,因为PropertyType
返回的类型从来不是字符串。我想你想要
!(result.GetType().GetProperty("Item").PropertyType == typeof(string))
请注意,我将强制转换为IEnumerable
,因为您正在通过反射查找属性,所以强制转换与此无关
回答第三次编辑
!((IEnumerable)result).GetType().GetProperty("Item").PropertyType is string)
我想检查基类的继承
对于您想要的类型。IsAssignableFrom()
:
您的意思是GetProperty(“Item”).PropertyType.IsValueType
?感谢您的回复!这正是我所需要的。但只是从OP中抽象出来。如果我需要检查IEnumerable是否属于某种类型(甚至是基类),该怎么办?因为参数的类型是object
任何值类型都将被装箱,所以您知道它不是值类型。您可以100%确定它始终是引用类型。如果这个方法是泛型的,那么它可以是。@Teejay啊,如果这是你的问题,那么答案就是某个东西是否是值类型并不是衡量它是否可以“分解”的标准。用户定义的结构可以包含可能需要进一步细分的字段/属性/方法,而用户定义的类也可以不包含这些字段/属性/方法。您的意思是GetProperty(“Item”).PropertyType.IsValueType
?感谢您的回复!这正是我所需要的。但只是从OP中抽象出来。如果我需要检查IEnumerable是否属于某种类型(甚至是基类),该怎么办?因为参数的类型是object
任何值类型都将被装箱,所以您知道它不是值类型。您可以100%确定它始终是引用类型。如果这个方法是泛型的,那么它可以是。@Teejay啊,如果这是你的问题,那么答案就是某个东西是否是值类型并不是衡量它是否可以“分解”的标准。用户定义的结构可以包含可能需要进一步细分的字段/属性/方法,而用户定义的类也可以不包含这些。哦,是的,对不起,在我写的代码中,我忘记了if(result is IEnumerable)
part:)需要更多的工作-IEnumerable。项
定义为对象
。如果您想知道它是否是IEnumerable
并且T
是值类型,您需要对泛型类型参数进行一些反射。不,“Item”只返回枚举器的类型,只是尝试了一下。@Teejay我错了-IEnumerable
根本没有Item
属性。幸运的是,基础类型是IEnumerable
和IEnumerable
,它们都具有强类型的项
属性。如果类型只是一个普通的IEnumerable
(就像ArrayList
)你会得到一个运行时异常。哦,是的,对不起,在我写的代码中,我忘记了If(result is IEnumerable)
部分:)需要更多的工作-IEnumerable。Item
被定义为对象
。如果您想知道它是否是IEnumerable
并且T
是值类型,您需要对泛型类型参数进行一些反射。不,“Item”只返回枚举器的类型,只是尝试了一下。@Teejay我错了-IEnumerable
根本没有Item
属性。幸运的是,基础类型是IEnumerable
和IEnumerable
,它们都具有强类型的项
属性。如果该类型只是一个普通的IEnumerable
(如ArrayList
),则会出现运行时异常。