I am trying to get the values from multiple properties in an entity framework object. There are 11 properties, each with a date assigned to it. I’ve tried using reflection but I keep getting an error ” Object does not match target type”
public void CheckWeekStatus()
{
var currentFlexi = from c in FlexiContext.FlexPeriods where c.FlexiCurrentYear == true select c;
FlexPeriod s = new FlexPeriod();
PropertyInfo[] properties = s.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var info in properties)
{
var o = info.GetValue(currentFlexi,null);
}
}
FlexPeriod is the type that contains all the properties. I can loop through the properties but obviously I’m doing something wrong with the way I’m trying to access the values. Any suggestions would be appreciated.
Firstly, you can get the
Typewithout instantiating an object:The reason
GetValueis failing is thatcurrentFlexiis a collection ofFlexPeriodobjects ( actually anIEnumerable<FlexPeriod>), not a single instance ofFlexPeriod.