2009-06-25 16 views
0

I ont la méthode suivante en C#:Comment utiliser les génériques C# dans Delphi 2009/2010?

public T Read<T>() 
    { 
     T[] t = new T[1]; 

     int s = Marshal.SizeOf(typeof(T)); 
     if (index + s > size) 
      throw new Exception("Error 101 Celebrity"); 

     GCHandle handle = GCHandle.Alloc(t, GCHandleType.Pinned); 
     Marshal.Copy(dataRead, index, handle.AddrOfPinnedObject(), s); 

     index += s; 

     return t[0]; 
    } 

dataRead est un octet tableau []. L'index et la taille sont de type entier.

La fonction lit un type à partir de dataRead (octet []) et augmente l'index (index + = type).

Tout sur le net, quand je google "Delphi génériques" - tout ce qu'il apparaît est Trecords et classes, ce qui n'est pas ce dont j'ai besoin.

Comment puis-je faire ce code dans Delphi?

Répondre

4
function TReader.Read <T>: T; 
begin 
    if FIndex + SizeOf (T) > Length (FDataRead) then 
    raise Exception.Create ('Error 101 Celebrity'); 
    Move (FDataRead[FIndex], Result, SizeOf (T)); 
    Inc (FIndex, SizeOf (T)); 
end; 
Questions connexes