【问题标题】:Reflection PropertyInfo GetValue call errors out for Collection<> type propertyCollection<> 类型属性的反射 PropertyInfo GetValue 调用错误
【发布时间】:2011-02-19 00:57:54
【问题描述】:

我有一个 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\n参数名称:源”

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

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

【问题讨论】:

    标签: .net exception reflection


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-10
      相关资源
      最近更新 更多