Reputation: 2031
I Define a Testclass like
MyTest = Class(TTestCLass)
procedure DoMyTest_1 (...);
procedure DoAnontherTest (...);
function OnemoreTest : Boolean;
.....
end;
Running Unit testing the testrunner framework shows the List of defined testfunctions and it is very easy to select individual test from the GUI / ListBox inside this framework. I would like to extract at run time the List of defined function from a class and be able to call this function dynamically.
The idea goes like this, but I do not know how to implement
procedure ExtractProcedureNamefromClass (aClass : TObject) : TStringlist ;
begin
?????
end;
procedure ClassaClassProcedureByName ( aClass : TObject ; FunctionName : String );
begin
/// can you do it more flexible
if Functionname=DoMyTest_1 then
MyClass.DoMyTest_1(...);
end;
Upvotes: 0
Views: 939
Reputation: 136441
Depending of your Delphi version you can use the RTTI.
like so
{$APPTYPE CONSOLE}
uses
RTTI,
Classes,
SysUtils;
var
LCtx : TRttiContext;
LMethod : TRttiMethod;
begin
try
LCtx:=TRttiContext.Create;
try
//list the methods for the TStrings class
for LMethod in LCtx.GetType(TStrings).GetMethods do
Writeln(LMethod.Name);
finally
LCtx.Free;
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
Upvotes: 4