【发布时间】:2013-09-28 21:52:59
【问题描述】:
如何根据下面的代码使用反射实例化一个数组属性?
public class Foo
{
public Foo()
{
foreach(var property in GetType().GetProperties())
{
if (property.PropertyType.IsArray)
{
// the line below creates a 2D array of type Bar. How to fix?
var array = Array.CreateInstance(property.PropertyType, 0);
property.SetValue(this, array, null);
}
}
}
public Bar[] Bars {get;set;}
}
public class Bar
{
public string Name {get;set;}
}
【问题讨论】:
标签: c# arrays reflection