首页

工作单位

管理书库

交流之窗

技术资料

应用案例

管理系统

留言薄

我的链接

  

如何用函数名字符串来调用函数?

delphilxh
比如我有一些函数(过程),我需要掉用他们,能不能通过函数名字符串来调用?有点儿象宏替换的意思!
type
{$M+}
TMyObj = class
published
function CommandOne: Integer;
function CommandTwo: Integer;
function CommandThree: Integer;
function CommandFour: Integer;
end;
{$M-}

function DoCommand1(const Command: string): Integer;
var
CommandProc: function: Integer of object;
begin
TMethod(CommandProc).Code := TMyObj.MethodAddress(Command);
if Assigned(TMethod(CommandProc).Code) then Result := CommandProc;
end;

--
WBR, LVT.

PS: A second method by V.Titov :

uses
TypInfo;

type
TCommand = (CommandOne, CommandTwo, CommandThree, CommandFour);

function DoCommand2(const Command: string): Integer;
begin
Result := 0;
case TCommand(GetEnumValue(TypeInfo(TCommand), Command)) of
CommandOne: ..;
CommandTwo: ..;
CommandThree: ..;
CommandFour: ..;
end;
end;