如果 我们某个方法中有几十个判断,判断的条件是 字符串比较,如何减少判断,直接定位到与 字符串相同的方法去执行?
下列方法 应该可行
MethodAddress:取得方法的地址,这个方法需要是published的。
实例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TShowInfo = procedure (info: string ) of object ; //声明一个procedure类型,参数和ShowInfo一致 TForm1 = class (TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } published procedure ShowInfo(info: string ); end ; var Form1: TForm1; implementation {$R *.dfm} { TForm1 } procedure TForm1 . ShowInfo(info: string ); begin ShowMessage(info); end ; procedure TForm1 . Button1Click(Sender: TObject); var s:TShowInfo; begin @s := MethodAddress( 'ShowInfo' ); //取得ShowInfo的地址 if @s <> nil then //如果不为空 begin s( '中华人民共和国' ); //执行 end ; end ; end . |