【问题标题】:sending a record type as parameter using dwscript使用 dwscript 发送记录类型作为参数
【发布时间】:2012-03-09 10:04:21
【问题描述】:

请考虑以下记录:

Type 
  TStudent = record
    Name:String;
    Age: Integer;
    Class:String;
  end;

我有一个类 TSchool,它具有以下功能:

function AddStudent(LStudent:TStudent):Boolean;

我想在 dwsunit 中使用这个类(TSchool),以及这个函数,但我不知道如何将记录类型作为参数发送。 这是我已经达到的程度:

procedure TForm1.dwsUnitClassesTSchoolMethodsAddStudentEval(Info: TProgramInfo;
  ExtObject: TObject);
begin
  Info.ResultAsBoolean:=(ExtObject as TSchool).AddStudent(Info.Vars['LStudent'].Value);
end;

但这不起作用,它不断给我关于不兼容类型的错误。

我还在 dwsunit 中定义了一个记录 TSchool,但这也不起作用。 任何帮助表示赞赏。

【问题讨论】:

  • "我想在dwsunit中使用这个类,并且这个函数也是"哪个类?还是您指的是唱片?
  • 你用的是什么Delphi版本?
  • 顺便说一句。您可以使用 RTTI 将 TSchool 暴露给脚本引擎
  • 我使用的是 Delphi 2010。如何使用 RTTI?我添加了 RTTIConnector 并将其连接到我的脚本,但不知道如何根据它编写我的代码,或者使用什么函数。我是 DWScript 新手。

标签: delphi class record dwscript


【解决方案1】:

我现在没有 Delphi 2010,但我有 Delphi XE(它应该也可以在 D2010 中工作),所以这对我有用,你当然可以修改为满足您的需求:

program Project1;

{$APPTYPE CONSOLE}


uses
   SysUtils
  ,Windows
  ,dwsComp
  ,dwsCompiler
  ,dwsExprs
  ,dwsCoreExprs
  ,dwsRTTIExposer
  ,Generics.Collections
  ;

//  required
{$RTTI EXPLICIT METHODS([vcPublic, vcPublished]) PROPERTIES([vcPublic, vcPublished])}
{M+}

type
  //  student definition
  TStudent = record
    Name: string;
    Age: Integer;
    AClass: string;
  end;

  //  student list, we use generics
  TStudentList = class(TList<TStudent>);

  //  school class
  TSchool = class(TObject)
  private
    FStudentList: TStudentList;
  published
    constructor Create;
    destructor Destroy; override;
  published
    procedure AddStudent(AStudent: TStudent);
    function GetStudentCount: Integer;
    function GetStudent(Index: Integer): TStudent;
  end;

{ TSchool }

procedure TSchool.AddStudent(AStudent: TStudent);
begin
  FStudentList.Add(AStudent);
end;

constructor TSchool.Create;
begin
  FStudentList := TStudentList.Create;
end;

function TSchool.GetStudentCount: Integer;
begin
  Result := FStudentList.Count;
end;

function TSchool.GetStudent(Index: Integer): TStudent;
begin
  Result := FStudentList[ Index ];
end;

destructor TSchool.Destroy;
begin
  FStudentList.Free;
  inherited;
end;

procedure TestRecords;
var
  LScript: TDelphiWebScript;
  LUnit: TdwsUnit;
  LProg: IdwsProgram;
  LExec: IdwsProgramExecution;
begin
  LScript := TDelphiWebScript.Create(NIL);
  LUnit := TdwsUnit.Create(NIL);
  try
    LUnit.UnitName := 'MySuperDuperUnit';
    LUnit.Script := LScript;

    //  expose TStudent record to the script
    LUnit.ExposeRTTI(TypeInfo(TStudent));
    //  expose TSchool class to script
    LUnit.ExposeRTTI(TypeInfo(TSchool));
    //  compile a simple script
    LProg := LScript.Compile(
      'var LSchool := TSchool.Create;'#$D#$A +
      'var LStudent: TStudent;'#$D#$A +
      'var Index: Integer;'#$D#$A +
      'for Index := 0 to 10 do begin'#$D#$A +
        'LStudent.Name := Format(''Student #%d'', [Index]);'#$D#$A +
        'LStudent.Age := 10 + Index;'#$D#$A +
        'LStudent.AClass := ''a-4'';'#$D#$A +
        'LSchool.AddStudent( LStudent );'#$D#$A +
      'end;'#$D#$A +
      'PrintLn(Format(''There are %d students in school.'', [LSchool.GetStudentCount]));'#$D#$A +
      'LStudent := LSchool.GetStudent( 5 );'#$D#$A +
      'PrintLn(''6th student info:'');'#$D#$A +
      'PrintLn(Format(''Name: %s''#$D#$A''Age: %d''#$D#$A''AClass: %s'', [LStudent.Name, LStudent.Age, LStudent.Aclass]));'
    );

    if LProg.Msgs.HasErrors then begin
      Writeln(LProg.Msgs.AsInfo);
      Exit;
    end;

    try
      LExec := LProg.Execute;
    except
      on E: Exception do
        WriteLn(E.Message + #$D#$A + LExec.Msgs.AsInfo );
    end;
    Writeln(LExec.Result.ToString);
  finally
    LScript.Free;
  end;
end;

begin
  try
    Writeln('press enter to begin');
    Readln;
    TestRecords;;
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

【讨论】:

  • 听起来很有趣,我试试看,回复一下,谢谢!!
  • 谢谢,一切顺利,工作正常。但我可以问一个额外的问题吗?为什么如果我有一个动态数组,我不能使用 ExposeRTTI 方法(错误是:无法公开类型的动态数组:记录)...谢谢
  • 请注意动态数组是记录的(例如:TClass: TStudent的数组
  • @Zeina 关于“动态类型数组”,对此添加了最低限度的支持,但仅适用于整数、字符串、浮点数和变体等简单类型,其他任何内容都未经测试
  • 非常感谢...我会努力解决的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
  • 2018-11-10
  • 1970-01-01
相关资源
最近更新 更多