Autor Beitrag
Mitmischer 1703
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 754
Erhaltene Danke: 19

Win 7, Debian
Delphi Prism, Delphi 7, RAD Studio 2009 Academic, C#, C++, Java, HTML, PHP
BeitragVerfasst: So 19.12.10 12:03 
Hi DF!

Ich habe folgende Klassen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
     TLibrary = class(TObject)
     public
       function HasMethod(AFunc : String) : Boolean;
       {...}
     end;

     TStdLibrary = class(TLibrary)     
     public
       function Rotate : Boolean;
       function Move : Boolean;
      {...}     
     end;


Die Implementation der HasMethod-Methode sieht wie folgt aus:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
function TLibrary.HasMethod(AFunc: String): Boolean;
begin
  Result := (MethodAddress(AFunc) <> nil);
end;


Sie ergibt jedoch immer false! Sogar bei folgendem Aufruf:

ausblenden Delphi-Quelltext
1:
2:
 Result := TStdLibrary.Create(Robot);
 Bla := Result.HasMethod('rotate');


Für mich ist das total unverständlich, da TStdLibrary die Methode ja besitzt! Aber HasMethod liefert false zurück...

Warum ergibt die Methode immer false?

_________________
Die Lösung ist nicht siebzehn.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 19.12.10 12:06 
Die Methoden stehen bei dir aber unter Public, daher werden keine RTTI Informationen in die Exe geschrieben. Du musst die unter published packen. Nur dann ist in der Exe zur Laufzeit der Methodennname noch bekannt (sonst ist die Methode nur noch eine Adresse).

Für diesen Beitrag haben gedankt: Mitmischer 1703
Mitmischer 1703 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 754
Erhaltene Danke: 19

Win 7, Debian
Delphi Prism, Delphi 7, RAD Studio 2009 Academic, C#, C++, Java, HTML, PHP
BeitragVerfasst: So 19.12.10 12:55 
funktioniert, danke! :)

_________________
Die Lösung ist nicht siebzehn.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 19.12.10 13:20 
Nebenbei sieht das so aus als könnten Interfaces dir weiterhelfen. Denn die sind ideal, wenn du mehrere Klassen in der selben Hierarchie hast, die teilweise eine Funktionalität implementieren.

Dann kannst du ein Interface IRotateMe oder so erstellen und zur Laufzeit nett anfragen, ob dieses Interface implementiert ist.

Wenn du dann noch die Referenzzählung aushebelst, indem du _AddRef usw. entsprechend selbst überschreibst, kannst du damit wie bisher arbeiten, musst aber nicht über die RTTI gehen und mit Strings auf Methodennnamen testen.