0

当暴露一个宿主类并从中调用一个过程时,我得到了这个异常:

第一次机会例外,$7513C41F。异常类 ECompileError 带有消息“没有名称为“GetUnitCount”的可访问成员”。处理 Project23.exe (12832)

如何解决这个问题?我正在使用来自 SVN 的最新版本。

program DWScript_Test;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  dwsComp,
  dwsCompiler,
  dwsExprs,
  dwsSymbols;


type
  TMyClass = class
    function GetUnitCount: Integer; // It does indeed exist..
  end;

type
  TTest = class
    DWS: TDelphiWebScript;
    dwsUnit: TdwsUnit;
    fStats: TMyClass;
    prog: IdwsProgram;
    exec: IdwsProgramExecution;
    procedure ExposeInstancesAfterInitTable(Sender: TObject);
    procedure Execute(aText: string);
  end;

function TMyClass.GetUnitCount: Integer;
begin
  Result := 4;
end;

procedure TTest.ExposeInstancesAfterInitTable(Sender: TObject);
begin
 dwsUnit.ExposeInstanceToUnit('fStats', 'TMyClass', fStats);
end;

procedure TTest.Execute(aText: string);
begin
  DWS := TDelphiWebScript.Create(nil);

  dwsUnit := TdwsUnit.Create(nil);
  dwsUnit.UnitName := 'Test';
  try
    fStats := TMyClass.Create;
    dwsUnit.Script := DWS;
    dwsUnit.ExposeClassToUnit(TMyClass, TObject);
    dwsUnit.OnAfterInitUnitTable := ExposeInstancesAfterInitTable;
    prog := DWS.Compile(aText);

    if prog.Msgs.Count = 0 then
    begin
      exec := prog.Execute;
      Writeln(exec.Result.ToString);
    end
    else
      Writeln(prog.Msgs.AsInfo);
  finally
    dwsUnit.Free;
    DWS.Free;
  end;
  Readln;
end;

begin
  TTest.Create.Execute('PrintLn(IntToStr(fStats.GetUnitCount));');
end.
4

1 回答 1

0

您的成员函数确实存在,但它没有被 拾取ExposeClassToUnit,因为它不读取函数。

从上面的评论中的实现TdwsUnit.ExposeClassToUnit

AClass is the class to expose to the unit. All published properties of standard
simple datatypes that are supported in DWS will be exposed that were introduced
between AAncestor and AClass. 

所以它只获取属性,而不是函数,并且只获取访问级别已发布的那些。

于 2013-10-04T22:26:25.270 回答