型別與泛型標記

雲水浮萍發表於2015-03-28

uses System.TypInfo;

 

  // Sync with compiler internals
  TTypeKind = (tkUnknown, tkInteger, tkChar, tkEnumeration, tkFloat,
    tkString, tkSet, tkClass, tkMethod, tkWChar, tkLString, tkWString,
    tkVariant, tkArray, tkRecord, tkInterface, tkInt64, tkDynArray, tkUString,
    tkClassRef, tkPointer, tkProcedure {, tkMRecord});

type
{$IFDEF NEXTGEN}
  TSymbolName = Byte;
{$ELSE  NEXTGEN}
  TSymbolNameBase = string[255];
  TSymbolName = type TSymbolNameBase;
{$ENDIF NEXTGEN}

 

  TTypeInfoFieldAccessor = record
  strict private                   //靜態?私有
    FData: PByte;
  public
    procedure SetData(const Data: PByte); inline;
    class operator Equal(const Left, Right: TTypeInfoFieldAccessor): Boolean; inline;
    function UTF8Length: integer; inline;
    function ToString: string;
{$IFNDEF NEXTGEN}
    function ToShortUTF8String: ShortString; inline;
{$ENDIF !NEXTGEN}
    function ToByteArray: TBytes;
    function Tail: PByte; inline;
  end;

-------------------TypeInfo (T)-------------------

  PTypeInfo = ^TTypeInfo;
  TTypeInfo = record
    Kind: TTypeKind;
    Name: TSymbolName;
    function NameFld: TTypeInfoFieldAccessor; inline;
   {TypeData: TTypeData}
    function TypeData: PTypeData; inline;
  end;

 

function TSampleClass<T>.GetDataName: string;
var
 Kind: TTypeKind;
begin
  Result := GetTypeName (TypeInfo (T));
  Kind := PTypeInfo(TypeInfo(T))^.Kind;
  case Kind of
    tkInteger: ShowMessage('I');
    tkFloat: ShowMessage('F');
    tkString,tkUString: ShowMessage('S');
    tkChar:   ShowMessage('C');
    tkClass:  ShowMessage('Class');
    else ShowMessage(Result);
  end;
end;

相關文章