知道对象c#中的属性类型

时间:2011-07-01 21:26:19

标签: c# reflection types properties

我知道如何使用反射来获取对象属性:

var properties = typeof(T).GetProperties();

现在我怎么知道properties [0]是否是一个字符串?或者它可能是一个int?我怎么知道?

1 个答案:

答案 0 :(得分:10)

properties的每个元素都是PropertyInfo,其PropertyType属性,表示属性的类型。

例如,您可以使用:

if (properties[0].PropertyType == typeof(string))

或者如果您想以继承许可的方式检查某些内容:

if (typeof(Stream).IsAssignableFrom(properties[0].PropertyType))
相关问题