delphi - 使用 dwscript 发送记录类型作为参数

标签 delphi class record dwscript

请考虑此记录:

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

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

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,但这也不起作用。 感谢任何帮助。

最佳答案

我现在没有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.

关于delphi - 使用 dwscript 发送记录类型作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9632173/

相关文章:

delphi - 将任何记录转换为字符串并返回?

delphi - 我是否只能使用Delphi 7下Controls.pas中定义的光标编号?

delphi - 命令历史记录单元的意外行为

delphi - 我应该用什么delphi代码替换对已弃用的TThread方法Suspend的调用?

c++ - 如果我们可以实例化类的对象并访问类的成员,为什么我们需要一个指向类的指针?

c++ - new Object() 和 Object() 有什么区别

Delphi 7 应用程序最佳实践/必备

c# - 从另一个类访问公共(public) by​​te[] 数组

haskell - 禁止 Haskell 中给定类型的记录更新

variables - 捕获Lua中的变量变化