vendredi 19 janvier 2018

Initializing all property lists in constructor with reflection c#

I am trying to initialize all properties in class (lists) with using reflection:

public class EntitiesContainer
{
    public IEnumerable<Address> Addresses { get; set; }
    public IEnumerable<Person> People { get; set; }
    public IEnumerable<Contract> Contracts { get; set; }

    public EntitiesContainer()
    {
        var propertyInfo = this.GetType().GetProperties();
        foreach (var property in propertyInfo)
        {
            property.SetValue(property, Activator.CreateInstance(property.GetType()), null);
        }
    }
}

I am getting exception:

No constructor has been defined for this object without parameters.

I would appreciate tips.





Aucun commentaire:

Enregistrer un commentaire