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

Delphi 如何在执行";之前将正确类型的对象加载到集合中;TRead.ReadProp";程序,delphi,collections,components,Delphi,Collections,Components,我正在集合项中创建一组属性。根据其类型,每个项目都有一组不同的属性: 类型 TMyProps=类(TPersistent) 私有的 fcomom:布尔型; 过程Setcommom(常量值:布尔值); 出版 属性commom:boolean read fcomm write Setcommom; 结束; TMyPropsClass=TMyProps的类别; TFieldPropsFloat=类(TMyProps) 私有的 FDecimalplaces:整数; 过程SetDecimalplaces(

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

类型
TMyProps=类(TPersistent)
私有的
fcomom:布尔型;
过程Setcommom(常量值:布尔值);
出版
属性commom:boolean read fcomm write Setcommom;
结束;
TMyPropsClass=TMyProps的类别;
TFieldPropsFloat=类(TMyProps)
私有的
FDecimalplaces:整数;
过程SetDecimalplaces(常量值:整数);
出版
属性Decimalplaces:整数读取FDecimalplaces写入SetDecimalplaces;
结束;
TFieldPropsStr=类(TMyProps)
私有的
长度:整数;
过程SetLength(常量值:整数);
出版
属性长度:整数读取长度写入设置长度;
结束;
TMyCollection=类别(TOwnedCollection)
结束;
TMyItem=类(TCollectionItem)
私有的
FMyPropsClass:TMyPropsClass;
FMyProps:TMyProps;
程序ReadMyProps(读取器:TReader);
程序WriteMyProps(编写者:TWriter);
程序重新创建MyProps;
程序SetMyProps(常量值:TMyProps);
程序设置MyPropsClass(常量值:TMyPropsClass);
受保护的
程序定义属性(文件提交人:TFiler);推翻
公众的
施工后的程序;推翻
出版
属性MyPropsClass:TMyPropsClass读取FMyPropsClass写入设置MyPropsClass;
属性MyProps:TMyProps read FMyProps write SetMyProps storage false;
结束;
在“TMyItem”中,加载写入“.dfm”文件的属性时出错,因为尚未使用尚未从“.dfm”加载的“MyPropsClass”属性生成“MyProps”

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

编辑:另外,我正试着按照Remy Lebeau给我的提示(下面是评论),但是,我不能把清单上的每一项都写进去

//。。。
程序登记册;
实施
程序登记册;
开始
注册表组件('Samples',[TMyComponent]);
结束;
程序TMyItem.AfterConstruction;
开始
继承;
FMyPropsClass:=TFieldPropsStr;
重建我的道具;
结束;
程序TMyItem.定义属性(文件提交人:TFiler);
开始
继承的定义属性(文件管理器);
Filer.DefineProperty('MyProps',ReadMyProps,WriteMyProps,True);
结束;
类型
TReaderAccess=类别(踏板);
TWriterAccess=类(TWriter);
程序TMyItem.ReadMyProps(读取器:TReader);
开始
MyProps:=TMyPropsClass(FindClass(Reader.ReadString)).Create;
读卡器。校验值(vaCollection);
Reader.ReadListBegin;
而不是Reader.EndOfList
TReaderAccess(Reader).ReadProperty(MyProps);
Reader.ReadListEnd;
Reader.ReadListEnd;
结束;
程序TMyItem.RecreateMyProps;
开始
如果FMyProps为零,则
FMyProps.Free;
FMyProps:=FMyPropsClass.Create;
结束;
程序TMyItem.SetMyProps(常数值:TMyProps);
开始
FMyProps:=值;
结束;
程序TMyItem.SetMyPropsClass(常数值:TMyPropsClass);
开始
如果为FMyPropsClass值,则
开始
FMyPropsClass:=值;
重建我的道具;
结束;
结束;
程序TMyItem.WriteMyProps(编写者:TWriter);
开始
Writer.WriteString(MyProps.ClassName)//如果评论这行,写得很好
TWriterAccess(Writer).WriteValue(vaCollection);
Writer.WriteListBegin;
Writer.WriteProperties(MyProps);
作家、作家、听众;
作家、作家、听众;
结束;
{TMyProps}
过程TMyProps.Setcommom(常量值:布尔值);
开始
fcomm:=值;
结束;
{TFieldPropsFloat}
过程TFieldPropsFloat.SetDecimalplaces(常量值:整数);
开始
FDecimalplaces:=值;
结束;
{TFieldPropsStr}
过程TFieldPropsStr.SetLength(常量值:整数);
开始
长度:=数值;
结束;
{TButton1}
程序TMyComponent.AfterConstruction;
开始
继承;
FMyCollection:=TMyCollection.Create(Self,TMyItem);
结束;
程序TMyComponent.SetMyCollection(常量值:TMyCollection);
开始
FMyCollection:=值;
结束;

如何正确地为每个集合项执行
ReadMyProps
WriteMyProps
过程?

MyProps
属性标记为(或根本不将其发布),然后覆盖虚拟
DefineProperties()
方法以手动流化
MyProps
数据。请参见Embarcadero的DocWiki和Delphi Codesmith博客

例如:

类型
TMyItem=类(TCollectionItem)
私有的
程序ReadMyProps(读取器:TReader);
程序WriteMyProps(编写者:TWriter);
受保护的
程序定义属性(文件提交人:TFiler);推翻
出版
MyPropsClass:TMyPropsClass;
MyProps:TMyProps存储为false;
结束;
程序TMyItem.定义属性(文件提交人:TFiler);
开始
继承的定义属性(文件管理器);
Filer.DefineProperty('MyProps',ReadMyProps,WriteMyProps,True);
结束;
类型
TReaderAccess=类别(踏板);
TWriterAccess=类(TWriter);
程序TMyItem.ReadMyProps(读取器:TReader);
开始
MyProps:=TMyPropsClass(FindClass(Reader.ReadString)).Create;
读卡器。校验值(vaCollection);
Reader.ReadListBegin;
而不是Reader.EndOfList
TReaderAccess(Reader).ReadProperty(MyProps);
Reader.ReadListEnd;
Reader.ReadListEnd;
结束;
程序TMyItem.WriteMyProps(编写者:TWriter);
开始
Writer.WriteString(MyProps.ClassName);
TWriterAccess(Writer).WriteValue(vaCollection);
Writer.WriteListBegin;
Writer.WriteProperties(MyProps);
作家、作家、听众;
作家、作家、听众;
结束;

MyProps
属性标记为
stored=false
(或者根本不要将其
发布
),然后覆盖虚拟方法以手动流式传输
MyProps
数据。请参阅:在Embarcadero的DocWiki中了解更多详细信息,但我需要“MyProps”在object Inputer中可见。你的建议能做到这一点吗?“我需要这一点