开发者

Know the types of properties in an object c#

I know how to get an object properties using reflection:

var properties = typeof(T).GetProper开发者_如何学运维ties();

now how do I know if properties[0] is a string? or maybe it is a int? how can I know?


Each element of properties will be a PropertyInfo, which has a PropertyType property, indicating the type of the property.

So for example, you might use:

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

or if you wanted to check something in an inheritance-permitting way:

if (typeof(Stream).IsAssignableFrom(properties[0].PropertyType))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜