开发者

Know the types of properties in an object c#

开发者 https://www.devze.com 2023-03-17 00:02 出处:网络
I know how to get an object properties using reflection: var properties = typeof(T).GetProper开发者_如何学运维ties();

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

精彩评论

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

关注公众号