开发者

Reflection PropertyInfo GetValue call errors out for Collection<> type property

开发者 https://www.devze.com 2022-12-30 07:48 出处:网络
I have a propertyInfo object and I try to do a GetValue using it. object source = mysourceObject //This object has a prop开发者_JAVA百科erty \"Prop1\" of type Collection<>.

I have a propertyInfo object and I try to do a GetValue using it.

object source = mysourceObject //This object has a prop开发者_JAVA百科erty "Prop1" of type Collection<>.

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

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

// do whatever with propValue
// ...

I get error at the GetValue() call as "Value cannot be null.\r\nParameter name: source"

"Prop1" is a plain property declared as 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}


You need to get the property value for source, not this :

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

精彩评论

暂无评论...
验证码 换一张
取 消