私は長い間、C++プログラマには新しく、一度.selectedObjectsを使ってプロパティグリッドを初期化することを考えています。プロパティグリッドに現在の値の内容を取得する方法はありますか?PropertyGridの内容を取得するには?
ベン
私は長い間、C++プログラマには新しく、一度.selectedObjectsを使ってプロパティグリッドを初期化することを考えています。プロパティグリッドに現在の値の内容を取得する方法はありますか?PropertyGridの内容を取得するには?
ベン
PropertyGridは、内部構造を消費者に公開しません。
しかし、.Netでは、クラスプロパティを含むコードの構造(およびその部分を実行する)を調べるために "Refelction"を実行できます。
Hereは、反射の基礎を説明する記事です。実際には、プロパティのグリッドが表示するものより多くの内部構造を反映させることができます。
グリッド内のオブジェクトのすべてのプロパティを反復処理する必要があります。オブジェクトのタイプに基づく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"
}
質問を明確にすることはできますか? SelectedObjectを設定している場合、PropertyGridが変更しているオブジェクトにアクセスできますか? – jasonh