views:

260

answers:

2

Hello,

I have the following method in 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 is a byte[] array. index and size are integer type.

The function reads a type from dataRead(byte[]) and increases the index(index+=type).

All over the net,when I google "Delphi generics" - all it appears is Trecords and classes,which is not what I need.

How do I make that code in Delphi?

A: 

A similar question was asked about Delphi Generics, you may want to take a look.

James
+4  A: 
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;
Moritz Beutel