Delphi 获取持久对象的实例,给定字符串中的标识符
在Delphi7中,给定字符串中的对象标识符,如何获取持久对象的实例Delphi 获取持久对象的实例,给定字符串中的标识符,delphi,runtime,Delphi,Runtime,在Delphi7中,给定字符串中的对象标识符,如何获取持久对象的实例 function TForm1.GetObject(Identifier: string): TPersistent; begin //what to do here? end; 使用示例: //If I have these declared... public MyString: string; MyStringList: TStringList; //the function will be used s
function TForm1.GetObject(Identifier: string): TPersistent;
begin
//what to do here?
end;
使用示例:
//If I have these declared...
public
MyString: string;
MyStringList: TStringList;
//the function will be used something like this
MyString:=TStringList(GetObject('MyStringList')).Text;
提前谢谢你,请原谅我不能用英语清楚地表达我的问题 您可以创建一个已发布的属性,该属性可以通过运行时类型信息(RTTI)访问。见和的第73页 这是很常见的。 您需要按名称保存对象实例的列表。您已经在字符串列表中提出了此建议。这可用于按名称检索实例。因此: 创建对象时,您需要执行以下操作:
MyObjList := TStringList.Create;
MyObj := TMyObj.Create;
MyObjList.AddObject( 'Thing', MyObj );
MyObj2 := TMyObj.Create;
MyObjList.AddObject( 'Thing2', MyObj2 );
等等
现在,要检索,只需执行以下操作:
function GetObject( const AName : string ) : TMyObj;
begin
I := MyObjList.IndexOf( AName );
If I = -1 then
Raise Exception.Create( 'Cant find it' );
Result := MyObjList[I] as TMyObj;
end;
布里
function GetObject( const AName : string ) : TMyObj;
begin
I := MyObjList.IndexOf( AName );
If I = -1 then
Raise Exception.Create( 'Cant find it' );
Result := MyObjList[I] as TMyObj;
end;