2011-02-16 3 views

答えて

5

はい、反射を使用することは可能です。私はa factory to create dependency properties for WPFのために同様のものを実装しました。ソースコード全体はhereです。

コードの関連作品は:a factory to simplify creating view modelsを作成するときに、私は似た何かをしたので

// Check all properties for a dependency property attribute. 
const BindingFlags ALL_PROPERTIES = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; 
var matchingProperties = new Dictionary<PropertyInfo, DependencyPropertyAttribute>(); 
foreach (PropertyInfo property in m_ownerType.GetProperties(ALL_PROPERTIES)) 
{ 
    object[] attribute = property.GetCustomAttributes(typeof(DependencyPropertyAttribute), false); 
    if (attribute != null && attribute.Length == 1) 
    { 
     // A correct attribute was found. 
     DependencyPropertyAttribute dependency = (DependencyPropertyAttribute)attribute[ 0 ]; 

     // Check whether the ID corresponds to the ID required for this factory. 
     if (dependency.GetId() is T) 
     { 
      matchingProperties.Add(property, dependency); 
     } 
    } 
} 

一方私はすでに、抽象クラスの階層でこの動作を抽象化したが、私は上記のコードは、すでにあなたの質問に答えると信じています。この抽象的な 'factory'のソースコードはbe found hereです。

UPDATE:

PropertyInfo.GetValue()を使用し、プロパティの値にアクセスします。あなたは、あなたのクラスのインスタンスへの参照が必要です。

+0

ありがとう、それが動作するかどうかを確認するためにこれを実装するには時間がかかるかもしれないので、明日まで受け入れられないかもしれません:) – MrBliz

+0

@ Doozer1979:使用できる抽象クラスを追加しました。私のライブラリにいくつかの依存関係が見当たらないかもしれませんが、あなたが始めるかもしれません。 –

関連する問題