为兼容Delphi中的特有类型,Borland.Delphi.System单元中定义了很多类型别名。 如我们前面分析过的TObject就是System.Object的别名。 //-----------------------------------------Borland.Delphi.System.pas-- type TDateTime = type Double; Extended = type Double; // 80 bit reals are unique to the Intel x86 architecture Comp = Int64 deprecated;
同TObject一样,Delphi中异常类继承链的根Exception在Delphi.NET中,也只是BCL的异常类 System.Exception的一个别名,而只是通过class helper提供源代码级兼容性。 //-----------------------------------------Borland.Delphi.System.pas-- Exception = System.Exception; ExceptionHelper = class helper for Exception private class function CreateMsg(const Msg: string): Exception; function GetHelpContext: Integer; procedure SetHelpContext(AHelpContext: Integer); public // Doc: The help context return zero(0) if exception''''s helplink property // cannot be parsed into an integer. property HelpContext: Integer read GetHelpContext write SetHelpContext;
// constructor Create(const Msg: string) is provided by the CLR class class function CreateFmt(const Msg: string; const Args: array of const): Exception; class function CreateHelp(const Msg: string; AHelpContext: Integer): Exception; class function CreateFmtHelp(const Msg: string; const Args: array of const; AHelpContext: Integer): Exception; end; ExceptionClass = class of Exception;
type EAssertionFailed = class(Exception) public ShortMessage: string; Filename: string; LineNumber: Integer; end;
resourcestring SAssertionFailed = ''''%s (%s at %d)'''';
implementation
procedure _Assert(const Message, Filename: AnsiString; LineNumber: Integer); var LException: EAssertionFailed; begin { TODO : Should we be using System.Diagnostics.Debug.Assert/Fail? } {$MESSAGE WARN ''''Assert doesn''''''''t use CreateFmt because it returns the wrong type''''} LException := EAssertionFailed.Create(Format(SAssertionFailed, [Message, Filename, LineNumber])); LException.ShortMessage := Message; LException.Filename := Filename; LException.LineNumber := LineNumber; raise LException; end; //-----------------------------------------Borland.Delphi.System.pas-- _Assert函数的定义基本上是EAssertionFailed异常的一个简单包装。因为Delphi没有提供类似 C++中__FILE__、__LINE__之类的预定义宏,故而只能由编译器在用户使用到Assert函数时, 将当前文件名、行号等调试信息编译进代码中,即在编译器一级提供断言实现。
//-----------------------------------------Borland.Delphi.System.pas-- function Assigned(const AGCHandle: GCHandle): boolean; begin Result := AGCHandle.IsAllocated; end; //-----------------------------------------Borland.Delphi.System.pas--
function Random(const ARange: Integer): Integer; overload; function Random: Extended; overload;
implementation
var LastRandSeed: Integer = -1; RandomEngine: System.Random;
procedure InitRandom; begin if LastRandSeed <> RandSeed then begin if RandSeed = 0 then RandomEngine := System.Random.Create else RandomEngine := System.Random.Create(RandSeed); LastRandSeed := RandSeed; end; end;
procedure Randomize; begin LastRandSeed := -1; RandSeed := 0; end;
function Random(const ARange: Integer): Integer; begin InitRandom; Result := RandomEngine.Next(ARange); end;
function Random: Extended; begin InitRandom; Result := RandomEngine.NextDouble; end; //-----------------------------------------Borland.Delphi.System.pas—