反映PropertyInfo GetValue调用错误输出集合<>类型属性

时间:2010-05-13 10:30:09

标签: .net exception reflection

我有一个propertyInfo对象,我尝试使用它来进行GetValue。

object source = mysourceObject //This object has a property "Prop1" of type Collection<>.

var propInfo = source.GetType().GetProperty("Prop1");

var propValue = prop.GetValue(this, null);

// do whatever with propValue
// ...

我在GetValue()调用时得到错误,因为“值不能为空。\ r \ nParameter name:source”

“Prop1”是一个声明为Collection的普通属性。

prop.PropertyType = {Name =“Collection 1" FullName = "System.Collections.ObjectModel.Collection 1 [[Application1.DummyClass,Application1,Version = 1.5.5.5834,Culture = neutral,PublicKeyToken = 628b2ce865838339]]”} System.Type {System.RuntimeType }

1 个答案:

答案 0 :(得分:1)

您需要获取source的属性值,而不是this

var propValue = prop.GetValue(source, null);
相关问题