我正在尝试使用反射来初始化类(列表)中的所有属性:
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);
}
}
}
我遇到了异常:
没有为此对象定义不带参数的构造函数。
我将感激您提供的提示。
CreateInstance
中传递一些参数。其中一个类型需要更多的信息。 - Daniel A. Whitetypeof(List<>).MakeGenericType(...)
。 - Marc Gravell