rtti路由
/// <author>2023-1-26</author> unit rtti.execfunc; interface uses System.Classes, system.Rtti, System.StrUtils, System.SysUtils; type /// <code> /// 基类 /// </code> TFunc = class(TPersistent) end; /// <summary> /// 动态执行指定类的方法 /// </summary> /// <param name="className">类名</param> /// <param name="funcName">方法名</param> /// <param name="funcParam">方法参数</param> function execFunc(className, funcName: string; funcParam: array of TValue): TValue; implementation function FindAClass(const className: string): TClass; var ctx: TRttiContext; typ: TRttiType; list: TArray<TRttiType>; begin Result := nil; ctx := TRttiContext.Create; list := ctx.GetTypes; for typ in list do begin if typ.IsInstance and (EndsText(className, typ.Name)) then begin Result := typ.AsInstance.MetaClassType; break; end; end; ctx.Free; end; function execFunc(className, funcName: string; funcParam: array of TValue): TValue; begin var ctx: TRttiContext; var t: TRttiType; var m: TRttiMethod; ctx := TRttiContext.Create; var c: TClass := FindAClass(className); if c = nil then begin Writeln('还没有注册类: RegisterClass(TFunc1)'); Exit; end; t := ctx.GetType(c); m := t.GetMethod(funcName); var o: TFunc := c.Create as TFunc; Result := m.Invoke(o, funcParam); ctx.Free; o.Free; end; end.
标签:begin,end,ctx,className,var,rtti,typ,路由 From: https://www.cnblogs.com/hnxxcxg/p/17069179.html