gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:21:26 26 4
gpt4 key购买 nike

请考虑此记录:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com