使用Reflection设置类型为List 的Property

如何使用reflection创建具有自定义类(List )的通用List? 我需要能够添加值并使用propertyInfo.SetValue(..., ..., ...)来存储它。 将这些List 存储为其他数据结构会更好吗?

编辑:

我应该指定对象更像是这样,但Marc Gravell的答案仍然有用。

 class Foo { public List Bar { get; set; } } 

 class Foo { public string Bar { get; set; } } class Program { static void Main() { Type type = typeof(Foo); // possibly from a string IList list = (IList) Activator.CreateInstance( typeof(List<>).MakeGenericType(type)); object obj = Activator.CreateInstance(type); type.GetProperty("Bar").SetValue(obj, "abc", null); list.Add(obj); } } 

这是一个获取List <>类型并将其转换为List 的示例。

var list = typeof(List <>)。MakeGenericType(typeof(string));