rtti路由
/// <author>2023-2-10</author> fit delphi\lazarus
unit api.router;
{$IFDEF fpc}
{$MODE DELPHI}{$H+}
{$ENDIF}
interface
uses
Classes, Rtti, StrUtils, SysUtils;
type
/// <code>
/// 基类
/// </code>
TFunc = class(TPersistent);
/// <summary>
/// 动态执行指定类的方法
/// </summary>
function RouterAPI(className, funcName: string; funcParam: array of TValue): TValue;
implementation
function FindAClass(const className: string): TClass;
begin
result := nil;
if className = '' then exit;
result := TClass(Classes.FindClass(className));
end;
function RouterAPI(className, funcName: string; funcParam: array of TValue): TValue;
var ctx: TRttiContext;
t: TRttiType;
m: TRttiMethod;
c: TClass;
o: TFunc;
begin
ctx := TRttiContext.Create;
c := FindAClass(className);
if c = nil then
begin
Writeln('还没有注册类: RegisterClass(TFunc1)');
Exit;
end;
t := ctx.GetType(c);
m := t.GetMethod(funcName);
o := c.Create as TFunc;
Result := m.Invoke(o, funcParam);
ctx.Free;
o.Free;
end;
end.