2009-07-23 30 views
1

私は長い間、C++プログラマには新しく、一度.selectedObjectsを使ってプロパティグリッドを初期化することを考えています。プロパティグリッドに現在の値の内容を取得する方法はありますか?PropertyGridの内容を取得するには?

ベン

+0

質問を明確にすることはできますか? SelectedObjectを設定している場合、PropertyGridが変更しているオブジェクトにアクセスできますか? – jasonh

答えて

0

PropertyGridは、内部構造を消費者に公開しません。

しかし、.Netでは、クラスプロパティを含むコードの構造(およびその部分を実行する)を調べるために "Refelction"を実行できます。

Hereは、反射の基礎を説明する記事です。実際には、プロパティのグリッドが表示するものより多くの内部構造を反映させることができます。

0

グリッド内のオブジェクトのすべてのプロパティを反復処理する必要があります。オブジェクトのタイプに基づくReflectionを使用します。

object o = PropertyGrid.SelectedObject; 
Type t = o.GetType(); // We will work on type "t" 
List<MemberInfo> members = new List<MemberInfo>(); 
members.AddRange(t.GetProperties(BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance); // Get the public instance properties list 
foreach (MemberInfo member in members) 
{ 
    Type type = null; 
    object value = null; 
    PropertyInfo pi = (member as PropertyInfo); 
    type = pi.PropertyType; 
    if (type.IsSubclassOf(typeof(CollectionBase))) 
     continue; // Sorry 
    if (pi.GetCustomAttributes(typeof(NotSerializedAttribute), true).GetLength(0) > 0) 
     continue; 
    if (!pi.CanRead || !pi.CanWrite) 
     continue; 
    value = pi.GetValue(o, null); 
    // TODO Print out, or save the "value" 
} 
関連する問題