線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1212
推到 Plurk!
推到 Facebook!

不可能的任務? 能否動態列出所有 publish 了的 Method

答題得分者是:william
Justmade
版主


發表:94
回覆:1934
積分:2030
註冊:2003-03-12

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-05-09 13:09:17 IP:218.16.xxx.xxx 未訂閱
已知 : 1. 使用 RTTI 可動態列出大部份 publish 了的 properties 及 Event (除非是少部份不支援的形態) 2. 使用 TObject 的 MethodName 及 MethodAddress 可轉換一個 published Method 的 Name 及 Address 但若我相動態地列出一個 Object / Component / Form 所有 publish 了的 Method (Name/Address 均可),有沒有可能呢? 謝謝。
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-05-09 15:25:04 IP:147.8.xxx.xxx 未訂閱
Look for GetMethodList in clRTTI unit  < href="http://delphi.ktop.com.tw/topic.php?TOPIC_ID=30192">http://delphi.ktop.com.tw/topic.php?TOPIC_ID=30192
Justmade
版主


發表:94
回覆:1934
積分:2030
註冊:2003-03-12

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-05-09 16:08:27 IP:218.16.xxx.xxx 未訂閱
二個字 :    

震驚

天啊...
procedure GetMethodList( FromClass: TClass; MethodList: TStrings ); overload;
type
  PPointer = ^Pointer;
  PMethodRec = ^TMethodRec;  
  TMethodRec = packed record <---居然知道儲存 Publish Method 的內部 Record 架構
    wSize: Word;
    pCode: Pointer;
    sName: ShortString;
  end;
var
  MethodTable: PChar;
  AClass: TClass;
  MethodRec: PMethodRec;
  wCount: Word;
  nMethod: integer;
begin
  MethodList.Clear;
  AClass := FromClass;      while AClass <> nil do
  begin
    //Get a pointer to the class's published method table
    MethodTable := PChar( Pointer( PChar( AClass )   vmtMethodTable )^ ); 
    ↑ 要這樣取資料丫?? vmtMethodTable 又是甚麼怪獸誰會看system變數時知道丫        if MethodTable <> nil then
    begin
      //Get the count of the methods in the table
      Move( MethodTable^, wCount, 2 );          //Position the MethodRec pointer at the first method in the table
        //(skip over the 2-byte method count)
      MethodRec := PMethodRec( MethodTable   2 );          //Iterate through all the published methods of this class
      for nMethod := 0 to wCount - 1 do
      begin
        //Add the method name and address to the MethodList TStrings
        MethodList.AddObject( MethodRec.sName, MethodRec.pCode );
        //Skip to the next method
        MethodRec := PMethodRec( PChar( MethodRec )   MethodRec.wSize );
      end;
    end;
    //Get the ancestor (parent) class
    // The easy way:
    AClass := AClass.ClassParent;
  end;
end;
真的很可怕... 順帶一問,
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-05-09 16:43:33 IP:147.8.xxx.xxx 未訂閱
I think the virtual method table has not been changed too much since its introduction back in TP 5.5 (or even eariler?). There should be quite a few documentation on it and you may find the book 'Delphi in a nutshell' interesting... It has a few pages on RTTI. For other questions.... I cannot "蛇" too long, so sorry 
系統時間:2024-05-17 6:17:02
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!