如何从存储在D中的字符串中的名称调用函数 [英] How do I call a function from it's name stored in a string in D

查看:97
本文介绍了如何从存储在D中的字符串中的名称调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Delphi中,我可以使用它的名称动态调用函数:

In Delp I could call functions dynamically using it's name like this:

interface

type
  TQScrFuncType = function(Args: TQList): TQVar of Object;
  {$M+}
  TQScriptFunctions = class
  published
    function Add(Args: TQList): TQVar;
  public
    function Call(MName: String; Args: TQList): TQVar; Virtual;
  end;

implementation

function TQScriptFunctions.Add(Args: TQList): TQVar;
begin
  //Do something...
end;

function TQScriptFunctions.Call(MName: String; Args: TQList): TQVar;
var
  Md: TMethod;
begin
  Md.Code := Self.MethodAddress(MName);
  Md.Data := Pointer(Self);
  Result := TQScrFuncType(Md)(Args);
  Args.Free;
end;



TQScriptFunctions.Call可以从它的名字调用一个函数,我是尝试将它移植到D,D语言。

如何在D中执行此操作,我在字符串中有一个函数的名称,以及参数,我该如何调用该函数?



我尝试了什么:



我想把名字放好和指向数组中函数的指针,然后在数组中搜索该函数名,并通过它的指针执​​行该函数。它有效,但问题是我有超过100个功能,这会使事情更难,浪费内存,也许更慢(因为它会搜索数百个列表中的字符串)。 Delphi是本地编译的,D也应该有这样的方法。


The TQScriptFunctions.Call could call a function from it's name, I'm trying to port it to D, The D Language.
How do I do this in D, I have the name of a function in a string, and the arguments too, how do I call that function?

What I have tried:

I thought of putting the names and pointers to the functions in arrays, then search the array for that function name, and execute the function through it's pointer. It worked, but the problem is I have over 100 functions, that'll make things harder, and waste memory, and maybe slower (because it'll search for a string in a list of hundreds). Delphi is natively compiled, so is D, there should be a way to do this.

推荐答案

M +}
TQScriptFunctions = class
已发布
function 添加(Args:TQList):TQVar;
public
function 调用(MName: String ; Args:TQList):TQVar; 虚拟;
end ;

implementation

function TQScriptFunctions.Add( Args:TQList):TQVar;
开始
// 做某事......
end ;

function TQScriptFunctions.Call(MName: String ; Args:TQList): TQVar;
var
Md:TMethod;
begin
Md.Code:= Self .MethodAddress(MName);
Md.Data:=指针(自我);
结果:= TQScrFuncType(Md)(Args);
Args.Free;
end ;
M+} TQScriptFunctions = class published function Add(Args: TQList): TQVar; public function Call(MName: String; Args: TQList): TQVar; Virtual; end; implementation function TQScriptFunctions.Add(Args: TQList): TQVar; begin //Do something... end; function TQScriptFunctions.Call(MName: String; Args: TQList): TQVar; var Md: TMethod; begin Md.Code := Self.MethodAddress(MName); Md.Data := Pointer(Self); Result := TQScrFuncType(Md)(Args); Args.Free; end;



TQScriptFunctions.Call可以从它的名字调用一个函数,我是尝试将它移植到D,D语言。

如何在D中执行此操作,我在字符串中有一个函数的名称,以及参数,我该如何调用该函数?



我尝试了什么:



我想把名字放好和指向数组中函数的指针,然后在数组中搜索该函数名,并通过它的指针执​​行该函数。它有效,但问题是我有超过100个功能,这会使事情更难,浪费内存,也许更慢(因为它会搜索数百个列表中的字符串)。 Delphi是本地编译的,D也应该有这样的方法。


The TQScriptFunctions.Call could call a function from it's name, I'm trying to port it to D, The D Language.
How do I do this in D, I have the name of a function in a string, and the arguments too, how do I call that function?

What I have tried:

I thought of putting the names and pointers to the functions in arrays, then search the array for that function name, and execute the function through it's pointer. It worked, but the problem is I have over 100 functions, that'll make things harder, and waste memory, and maybe slower (because it'll search for a string in a list of hundreds). Delphi is natively compiled, so is D, there should be a way to do this.


这篇关于如何从存储在D中的字符串中的名称调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆