2009-08-05 6 views
1

Delphi 7では、永続オブジェクトのインスタンスを取得するには、オブジェクト識別子がstringに指定されていますか?文字列内の識別子を指定して、永続オブジェクトのインスタンスを取得します。

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; 

は、事前にありがとうと英語で明確に私の質問を表現することができないために私が謝罪してください。

答えて

1

実行時型情報(RTTI)を介してアクセスできる公開プロパティを作成できます。 Delphi in a nutshellおよびGetObjectPropのp.73を参照してください。

Writeln((GetObjectProp(O,'ObjField') As TNamedObject).ObjectName); 
2

これは非常に一般的です。 オブジェクトインスタンスのリストを名前で保持する必要があります。既にあなたの文字列リストでこれを提案しました。これは、名前でインスタンスを取得するために使用できます。だから、:あなたのオブジェクトを作成するとき はあなたが実行します。

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; 

ブリ

関連する問題