Dailycode.info

Short solution for short problems

A very easy and quick way to loop over Entity properties using reflection.

A very easy and quick way to loop over Entity properties using reflection. In the loop I loop over the properties and add some properties to a list. 

 

using System.Reflection;

 

var properties = typeof(MyEntity).GetProperties(BindingFlags.Public | BindingFlags.Instance);

foreach (var property in properties)

{

    var type = property.PropertyType;

    if (type == typeof(String) || type == typeof(Int16) || type == typeof(Int32) || type == typeof(Decimal?))

    {

        drpFields.Items.Add(new ListItem(property.Name, property.Name));

    }

}