Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 获取持久对象的实例,给定字符串中的标识符_Delphi_Runtime - Fatal编程技术网

Delphi 获取持久对象的实例,给定字符串中的标识符

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

在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 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;