2009-06-25 65 views
0

我在C#下面的方法:如何在delphi 2009/2010中使用C#泛型?

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是一個字節[]數組。 索引和大小是整數類型。

該函數從dataRead(byte [])讀取一個類型並增加索引(index + = type)。

遍佈網絡,當我谷歌「德爾福泛型」 - 它看起來是Trecords和類,這不是我所需要的。

如何在Delphi中創建該代碼?

回答

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;