Tono Nam
Tono Nam

Reputation: 36048

Know the types of properties in an object c#

I know how to get an object properties using reflection:

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

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

Upvotes: 3

Views: 126

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500525

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))

Upvotes: 10

Related Questions