0

私はここに「良い」問題があり、結果が得られずに多くの時間を費やしました。私はグーグルを調べたので...成功しませんでした。

デスクトップ(Windows7-64Bit)で.Net 4.0を使用しており、リフレクションを多用しています。

私の現在の問題は、独自のビジネス オブジェクトのインスタンスがあることです。インスタンスのすべてのプロパティにアクセスできるように、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.

両方 (プロパティと新しいインスタンス) の時間が同じであることはわかっています。

新しいインスタンスの作成とプロパティへの設定の間に多かれ少なかれステップを踏んで、何十回も試してみました...成功しませんでした。

前もって感謝します。

カーステン

4

0 に答える 0