I’m attempting to set a property on an object using reflection.
The property is an ICollection – if the Collection has not been instantiated, I want to get that done. My problems is that I’m having issues getting the inner type of the ICollection
This is my class
public class Report(){
public virtual ICollection<Officer> OfficerCollection { get; set; }
}
I’m trying to access the ‘Officer’ class defined below through reflection
public class Officer(){
public string Name{ get; set; }
}
Code snippet
Report report = new Report()
PropertyInfo propertyInfo = report.GetType().GetProperty("OfficerCollection");
object entity = propertyInfo.GetValue(report, null);
if (entity == null)
{
//How do I go about creating a new List<Officer> here?
}
Give this a whirl: