铸造集合
Collection<T>
到
ObservableCollection<T>
这样的:
ObservableCollection<MyObject> col1;
col1 = (ObservableCollection<MyObject>)new Collection<MyObject>();
但是当我尝试通过反射我收到以下错误做到这一点:
myProperty.SetValue(
element,
Convert.ChangeType(values, myProperty.PropertyType, null)
, null);
A first chance exception of type 'System.InvalidCastException' occurred in mscorlib.ni.dll Additional information: Object must implement IConvertible.
为什么在正常运行时的转换工作,而不是当我使用反射?
编辑 我结束了“解决”通过不使用Collection<T>
作为一种类型,而是直接使用我myProperty.PropertyType
类型我的问题。
该死的这是令人尴尬的。我有一个编译器错误没有演员,并错误地认为它会投的很好。 – 2014-11-08 16:24:11