相关疑难解决方法(0)

如何通过RTTI为TStringGrid.Cells之类的compex设置/获取属性值?

我有值存储在xml和lua代码中,并通过RTTI访问对象的属性.

var
  o, v: TValue; // o is current object
  a: TStringDynArray; // params as array
  ctx: TRttiContext;
  tt: TRttiType;
  p: TRttiProperty;
  pt: PTypeInfo;
begin
...
  ctx := TRttiContext.Create;
  try
    o := GetLastClassInParams(ctx, obj, a, param_idx);
    tt := ctx.GetType(o.TypeInfo);
    if high(a) < param_idx then
        raise Exception.Create(S_FN + S_NOP);
    p := tt.GetProperty(a[param_idx]);
    if p = nil then
        raise Exception.Create(S_FN + S_PNE + a[param_idx]);
    pt := p.PropertyType.Handle;
    case p.PropertyType.TypeKind of
      tkInteger: v := TValue.From<integer>(integer(Value));
      tkEnumeration: v := TValue.FromOrdinal(pt, GetEnumValue(pt, VarToStr(Value)));
      tkUString: v …
Run Code Online (Sandbox Code Playgroud)

delphi rtti

6
推荐指数
1
解决办法
7216
查看次数

使用RTTI获取/设置子属性

鉴于下面的代码片段,使用GetPropValue(MyComponent,'MySubComponent.Prop1')引发EPropertyError异常.如何使用GetPropValue/SetPropValue检索或设置SubProperties的值?

Type
  TMySubComponent = class(TInterfacedPersitent)
  private
    FProp1: Integer;
  published
    property Prop1: integer read FProp1 write FProp1;
  end;

  TMyComponent = class(TCompoent)
  private
    FMySubComponent : TMySubcomponent; 
  published
    property MySubComponent: TMySubComponent read FMySubComponent write FMySubComponent ;
  end;
Run Code Online (Sandbox Code Playgroud)

delphi rtti delphi-xe

3
推荐指数
1
解决办法
6271
查看次数

标签 统计

delphi ×2

rtti ×2

delphi-xe ×1