delphi通过方法名调用方法
delphi通过方法名调用方法
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type TFunc = procedure(Param: string) of object; //定义方法原型 TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); published procedure hello(p: string); //必须声明为published方法 end; var Form1: TForm1; implementation {$R *.dfm} //根据方法名调用方法 procedure ExecFunc(className, funcName, funcParam: string); var m: TMethod; f: TFunc; begin var p: TPersistentClass := FindClass(className); if p = nil then exit; m.Data := Pointer(p); m.Code := p.MethodAddress(funcName); if Assigned(m.Code) then begin f := TFunc(m); f(funcParam); end; end; { TForm1 } procedure TForm1.Button1Click(Sender: TObject); begin ExecFunc('TForm1', 'hello', 'hello yn'); end; procedure TForm1.hello(p: string); begin ShowMessage(p); end; initialization RegisterClass(TForm1); finalization UnRegisterClass(TForm1); end.
方法二:通过RTTI调用
unit Unit1; interface uses system.Rtti, System.StrUtils, Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type TFunc = class(TPersistent) //基类 end; TFunc1 = class(TFunc) //必须public,published procedure p1(p: string); procedure p2(p: string); end; TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private end; var Form1: TForm1; implementation {$R *.dfm} function FindAClass(const Name: 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(Name, typ.Name)) then begin Result := typ.AsInstance.MetaClassType; break; end; end; ctx.Free; end; procedure execFunc(className, funcName: string; funcParam: array of TValue); begin var ctx: TRttiContext; var t: TRttiType; var m: TRttiMethod; ctx := TRttiContext.Create; var c: TClass := FindAClass(className); t := ctx.GetType(c); m := t.GetMethod(funcName); var o: TFunc := c.Create as TFunc; m.Invoke(o, funcParam); ctx.Free; o.Free; end; { TForm1 } procedure TForm1.Button1Click(Sender: TObject); begin RegisterClass(TFunc1); //注册类 execFunc('TFunc1', 'p1', ['hello yn']); execFunc('TFunc1', 'p2', ['hello yn2']); end; { TFunc1 } procedure TFunc1.p1(p: string); begin ShowMessage(p); end; procedure TFunc1.p2(p: string); begin ShowMessage(p); end; end.
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/17066399.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2021-01-24 微服务概念讲解
2021-01-24 使用multipart打造rest粗接口