0

I am looping through a list of objects. Each object can contain one or more properties. I want to see if the property is either: a string, a numerical value, a boolean, or a Date time.

I did some research and found out to get the property type you can use:

object prop in item.GetType().GetProperties();

So I updated my looping to:

        foreach (var item in myGenericList)
        {
            foreach (object prop in item.GetType().GetProperties())
            {
                //Combobox
                if (prop.Equals(typeof(string)))
                {
                    GenerateComboBox(prop);
                }
                else if (prop is decimal || prop is int || prop is double)
                {
                    GenerateRangeControl(prop);
                }
                else if (prop is bool)
                {
                    GenerateToggle(prop);
                }
                else if (prop is DateTime)
                {
                    GenerateDatePicker(prop);
                }
            }
        }

But none of the conditions are being met. (No if-condition is true) How can I loop through a generic list of objects and find the data type of each property inside the object?

2
  • I forgot to mention when I do look at the value of the prop in debugging, the value is: {System.String FieldName} Aug 31, 2020 at 9:59
  • As per the manual: Returns PropertyInfo[] An array of PropertyInfo objects representing all public properties of the current Type.
    – TomTom
    Aug 31, 2020 at 10:00

2 Answers 2

3

GetProperties returns the properties themselves, as references PropertyInfo objects. A PropertyInfo isn't a string, or a decimal etc - it's the property, not the value of the property.

If you want to get the value of each property, you want something like:

foreach (var property in item.GetType().GetProperties())
{
    var propertyValue = property.GetValue(item);
    // Now use propertyValue
}

If you need to ignore setter-only properties (which are really rare) you can do that easily enough via filtering:

foreach (var property in item.GetType().GetProperties().Where(pi => pi.CanRead))
{
    var propertyValue = property.GetValue(item);
    // Now use propertyValue
}

If you don't need the value, just the type, you can use the PropertyType property to determine the type of the property.

2
  • This worked, thank you. I'll flag this as the correct answer as soon as I am allowed to Aug 31, 2020 at 10:08
  • @EmilevRooyen It will throw exception, if property has no getter. Also, make sure you actually want to execute the code in the property getter.
    – astef
    Aug 31, 2020 at 10:10
0

You need to check PropertyType:

if (prop.PropertyType == typeof(string))
{
    GenerateComboBox(prop);
}
else if (prop.PropertyType == typeof(decimal)
    || prop.PropertyType == typeof(int)
    || prop.PropertyType == typeof(double))
{
    GenerateRangeControl(prop);
}
// ... and so on

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.