TargetException - PropertyInfo.SetValue失败

时间:2013-06-18 12:17:57

标签: .net reflection propertyinfo setvalue

我有一个'好'的问题,我花了很多时间没有任何结果。我已经浏览了谷歌等等......没有任何成功。

我在桌面上使用.Net 4.0(Windows7-64Bit),我正在大量使用反射。

我目前的问题是我有一个自己的业务对象的实例。我正在使用PropertyInfo对象来访问实例的所有属性。一般情况下它运行良好,但有时候我的属性包含一个通用列表(或者一种(>>重新实现ObservableCollection<>)。当我尝试创建该属性类型的新实例并设置属性时实例到声明实例时,它以TargetException失败。

PropertyInfo pi = instance.GetType().GetProeperty("Items"); // instance is a class instance of 'SomeData' class, Items is a property (ObservableCollection<T>)
pi.SetValue(instance, System.Activator.CreateInstance(pi.PropertyType), null); // the new instance (of pi.PropertyType) will be created without any problem, but setting in instance with SetValue failed with a TargetException.

我知道(属性和新实例)都有相同的时间。

我尝试了多次,在创建新实例和将其设置为属性之间或多或少的步骤......没有任何成功。

提前致谢。

的Carsten

0 个答案:

没有答案