首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在执行"TRead.ReadProp“过程之前将正确类型的对象加载到集合中

如何在执行"TRead.ReadProp“过程之前将正确类型的对象加载到集合中
EN

Stack Overflow用户
提问于 2020-01-15 02:26:06
回答 1查看 74关注 0票数 0

我正在一个集合项中创建一组属性。每个项目根据其类型有一组不同的属性:

代码语言:javascript
复制
  type
    TMyProps = class(TPersistent)
  private
    Fcommom: boolean;
    procedure Setcommom(const Value: boolean);
    published
      property commom: boolean read Fcommom write Setcommom;
    end;
    TMyPropsClass = class of TMyProps;

    TFieldPropsFloat = class(TMyProps)
  private
    FDecimalplaces: integer;
    procedure SetDecimalplaces(const Value: integer);
    published
      property Decimalplaces: integer read FDecimalplaces write SetDecimalplaces;
    end;

    TFieldPropsStr = class(TMyProps)
  private
    FLength: integer;
    procedure SetLength(const Value: integer);
    published
      property Length: integer read FLength write SetLength;
    end;

    TMyCollection = class(TOwnedCollection)
    end;

    TMyItem = class(TCollectionItem)
    private
      FMyPropsClass: TMyPropsClass;
      FMyProps: TMyProps;
      procedure ReadMyProps(Reader: TReader);
      procedure WriteMyProps(Writer: TWriter);
      procedure RecreateMyProps;
    procedure SetMyProps(const Value: TMyProps);
    procedure SetMyPropsClass(const Value: TMyPropsClass);
    protected
      procedure DefineProperties(Filer: TFiler); override;
    public
      procedure AfterConstruction; override;
    published
      property MyPropsClass: TMyPropsClass read FMyPropsClass write SetMyPropsClass;
      property MyProps: TMyProps read FMyProps write SetMyProps stored false;
    end;

在“”TMyItem“”中,加载写入“”.dfm“”文件的属性时出错,因为“”MyProps“”尚未使用尚未从“”.dfm“”加载的“”MyPropsClass“”属性生成“”

如何解决?这是最好的方法吗?

编辑:此外,我正在尝试遵循Remy Lebeau给我的建议(评论如下),但是,我不能写下列表中的每一项。

代码语言:javascript
复制
///...

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TMyComponent]);
end;


procedure TMyItem.AfterConstruction;
begin
  inherited;
  FMyPropsClass := TFieldPropsStr;
  RecreateMyProps;
end;

procedure TMyItem.DefineProperties(Filer: TFiler);
begin
  inherited DefineProperties(Filer);
  Filer.DefineProperty('MyProps', ReadMyProps, WriteMyProps, True);
end;

type
 TReaderAccess = class(TReader);
 TWriterAccess = class(TWriter);

procedure TMyItem.ReadMyProps(Reader: TReader);
begin
  MyProps := TMyPropsClass(FindClass(Reader.ReadString)).Create;
  Reader.CheckValue(vaCollection);
  Reader.ReadListBegin;
  while not Reader.EndOfList do
    TReaderAccess(Reader).ReadProperty(MyProps);
  Reader.ReadListEnd;
  Reader.ReadListEnd;
end;

procedure TMyItem.RecreateMyProps;
begin
  if FMyProps <> nil then
     FMyProps.Free;

  FMyProps := FMyPropsClass.Create;
end;

procedure TMyItem.SetMyProps(const Value: TMyProps);
begin
  FMyProps := Value;
end;

procedure TMyItem.SetMyPropsClass(const Value: TMyPropsClass);
begin
  if FMyPropsClass <> Value then
  begin
    FMyPropsClass := Value;
    RecreateMyProps;
  end;
end;

procedure TMyItem.WriteMyProps(Writer: TWriter);
begin
  Writer.WriteString(MyProps.ClassName); //if comments this line, write fine

  TWriterAccess(Writer).WriteValue(vaCollection);
  Writer.WriteListBegin;
  Writer.WriteProperties(MyProps);
  Writer.WriteListEnd;
  Writer.WriteListEnd;
end;


{ TMyProps }

procedure TMyProps.Setcommom(const Value: boolean);
begin
  Fcommom := Value;
end;

{ TFieldPropsFloat }

procedure TFieldPropsFloat.SetDecimalplaces(const Value: integer);
begin
  FDecimalplaces := Value;
end;

{ TFieldPropsStr }

procedure TFieldPropsStr.SetLength(const Value: integer);
begin
  FLength := Value;
end;

{ TButton1 }

procedure TMyComponent.AfterConstruction;
begin
  inherited;
  FMyCollection := TMyCollection.Create(Self, TMyItem);
end;

procedure TMyComponent.SetMyCollection(const Value: TMyCollection);
begin
  FMyCollection := Value;
end;

如何正确地为集合的每一项实现ReadMyPropsWriteMyProps过程?

EN

回答 1

Stack Overflow用户

发布于 2020-01-22 04:54:03

MyProps属性标记为stored=false (或者根本不将其设置为published ),然后覆盖虚拟DefineProperties()方法以手动流式传输MyProps数据。请参阅Embarcadero的DocWiki中的Storing and Loading Unpublished Properties: Overriding the DefineProperties Method,以及Delphi Codesmith博客上的Streaming non-published TPersistent Properties – A Better Way

例如:

代码语言:javascript
复制
type
  TMyItem = class(TCollectionItem)
  private
    procedure ReadMyProps(Reader: TReader);
    procedure WriteMyProps(Writer: TWriter);
  protected
    procedure DefineProperties(Filer: TFiler); override;
  published
    MyPropsClass: TMyPropsClass;
    MyProps: TMyProps stored false;
  end;

procedure TMyItem.DefineProperties(Filer: TFiler);
begin
  inherited DefineProperties(Filer);
  Filer.DefineProperty('MyProps', ReadMyProps, WriteMyProps, True);
end;

type
 TReaderAccess = class(TReader);
 TWriterAccess = class(TWriter);

procedure TMyItem.ReadMyProps(Reader: TReader);
begin
  MyProps := TMyPropsClass(FindClass(Reader.ReadString)).Create;
  Reader.CheckValue(vaCollection);
  Reader.ReadListBegin;
  while not Reader.EndOfList do
    TReaderAccess(Reader).ReadProperty(MyProps);
  Reader.ReadListEnd;
  Reader.ReadListEnd;
end;

procedure TMyItem.WriteMyProps(Writer: TWriter);
begin
  Writer.WriteString(MyProps.ClassName);
  TWriterAccess(Writer).WriteValue(vaCollection);
  Writer.WriteListBegin;
  Writer.WriteProperties(MyProps);
  Writer.WriteListEnd;
  Writer.WriteListEnd;
end;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59739699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档