0

リストしたいいくつかの公開されたプロパティを持つクラスがあります。プロパティは、DevExpress スタイルからの TcxCustomStyle タイプです。次のコードを使用して名前を memdata テーブルに追加し、関連する TcxCustomStyle をすべて削除すると問題なく動作します。

問題は、TcxCustomStyle 型のプロパティの値を取得する方法です。

ほとんどの場合、それは私の側の単純なエラーですが、何が原因かわかりません。

procedure TfrmMain.ListProperties;
var
  ctx: TRttiContext;
  rType: TRttiType;
  rProp: TRttiProperty;
  i: integer;
  Value: TcxCustomStyle;
begin
  i := 1;
  memProperties.DisableControls;
  try
    memProperties.Close;
    memProperties.Open;

    rType := ctx.GetType(Settings.Styling.ClassType);
    for rProp in rType.GetProperties do
      begin
        Value := TcxCustomStyle(rProp.GetValue(Self).AsObject);
        memProperties.AppendRecord([i, rProp.Name, Value.Name]);
        Inc(i);
      end;

  finally
    ctx.Free;
    memProperties.EnableControls;
  end;
end;
4

2 に答える 2

1

多くの詳細が欠落しているため、何が間違っているのかを確認するのは少し難しい. 特に、タイプに関する情報が含まれておらず、エラーメッセージも含まれていないという事実。

私に飛び出すのは、 でrType指定されたタイプに設定されていることですSettings.Styling.ClassType。次に、そのプロパティを反復処理しSelf、タイプのインスタンスからそれらを読み取りますTfrmMain。それは間違っているようです。に渡すパラメーターGetValueは、型である必要がありますSettings.Styling.ClassType。に別のインスタンスを渡す必要があると思いますGetValue

unchecked cast の使用についても質問しますTcxCustomStyle(...)。それはあなたにとって難しいことです。チェックされたキャストを使用してください: ... as TcxCustomStyle.

また、コードでは、 のすべてのプロパティがSettings.Styling.ClassType型であると想定していますTcxCustomStyle。おそらくそれは合理的な仮定ですが、私にはわかりません。

于 2013-06-16T17:20:14.240 に答える
0

古いコードを調べた後、関数を次の作業コードに書き直しました。あとは、各プロパティの説明をクラスに保存する方法を理解するだけです。TcxCustomStyle 値と、表示できる説明の両方が必要です。しかし、それはまったく別の質問です。

procedure TfrmMain.ListProperties;
var
  ctx       : TRttiContext;
  lType     : TRttiType;
  lProperty : TRttiProperty;
  i         : integer;
  Value     : TcxCustomStyle;
begin
  i := 1;
  memProperties.DisableControls;
  ctx := TRttiContext.Create;
  try
    memProperties.Close;
    memProperties.Open;

    lType := ctx.GetType(Settings.Styling.ClassType);
    for lProperty in lType.GetProperties do
      begin
        Value := TcxCustomStyle(lProperty.GetValue(Settings.Styling).AsObject);
        memProperties.AppendRecord([i, lProperty.Name, Value.Name]);
        Inc(i);
      end;

  finally
    ctx.Free;
    memProperties.EnableControls;
  end;
end;
于 2013-06-16T19:42:24.557 に答える