2012-03-09 51 views
2

請考慮此記錄發送記錄類型作爲參數TSchool)在dwsunit,而這個功能太多,但我無法弄清楚如何發送記錄類型作爲參數。 這是我已經達到多遠:使用dwscript

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

,但是這是行不通的,它不斷給我約不兼容類型的錯誤。

我也曾在dwsunit記錄TSchool定義,但是這也不能工作。 任何幫助表示讚賞。

+0

「我想在dwsunit中使用這個類,而且這個函數太」哪個類?或者你是指記錄? – ComputerSaysNo 2012-03-09 12:46:48

+0

對不起,我會編輯任務。 – Zeina 2012-03-09 12:50:02

+0

您使用的是哪個Delphi版本? – ComputerSaysNo 2012-03-09 12:54:58

回答

2

我現在沒有德爾福2010年,但我確實有德爾福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. 
+0

聽起來有趣,生病了試試看,並回復,謝謝! – Zeina 2012-03-09 13:47:37

+0

謝謝,一切都很好,並正常工作。但是我可以問一個額外的問題嗎?爲什麼如果我有一個動態數組,我不能使用ExposeRTTI方法(誤差爲:**不能暴露類型的動態數組:記錄**)...謝謝 – Zeina 2012-03-10 07:22:25

+0

請注意動態數組是記錄(前:** TClass:TStudent **) – Zeina 2012-03-10 07:30:13

相關問題