Re: using generics to serialize primitives.



public class Sizeof<T>
{
public static bool IsBinary = typeof(T).IsPrimitive||
(typeof(T).IsValueType &&
typeof(T).StructLayoutAttribute.Pack == 1&&
typeof (T).StructLayoutAttribute.Value!= LayoutKind.Auto);
public static int Size = GetSize();
public static int GetSize()
{
if (IsBinary)
return Marshal.SizeOf(typeof(T));
return 1; //partialy safe defualt.
}
}
unsafe public static byte[] ToBytes<T>(void * ptr)
{
int bytes = Sizeof<T>.Size;
Debug.Assert(Sizeof<T>.IsBinary);
byte[] buff = new byte[bytes];
Copy((byte*)ptr, buff, 0, bytes);
return buff;
}

I wrote this unsafe method, but I never fully trusted it ...
you have to specify the generic type explicitly when you call it.
its also awkward to use as you cant take the address of a generic parameter.
it stores the size in a static field for quick access.

I ended up just using overloaded functions wich call BitConverter.

Colin =^.^=

"Brian" <Brian@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:483C27B7-18FC-4B30-960D-E57C522D8C4F@xxxxxxxxxxxxxxxx
What is the easiest way to convert primitives to a byte array? I tried the
BinaryFormatter serialization but it serializes objects so when I
serialized
an and int it took 54 bytes instead of 4. I tried something like the
following but it won't compile (I admit I use generics a lot but haven't
written many so this may be way off):

class X<T>
{
public byte[] ToByteArray(T val)
{
BinaryWriter bw = new BinaryWriter(new MemoryStream());
bw.Write(val);
BinaryReader br = new BinaryReader(bw.BaseStream);
br.BaseStream.Position = 0;
return br.ReadBytes((int)br.BaseStream.Length);
}
}

I don't want to have an overloaded method for every primitive type. Is
there
an easy way to do this...or have the overloads already been written to do
this and I am just missing it? It seems like a pretty basic task that
should
be there somewhere already.

thanks


.



Relevant Pages

  • Re: serialization without preregistering subclasses
    ... Why not just use the BinaryFormatter class? ... The serialization engine will handle all of these issues for you. ... public class datamessage: message ... subclass for binary serialization of a message that starts with the ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Generics - A question on generics - delegates - runtime binding.
    ... public class Foowhere T: ... turn we promise that whenever we instantiate a Foo we will pass a T that ... > public static int GetValue ... > public static float GetValue(Avar) ...
    (microsoft.public.dotnet.languages.csharp)
  • function in Derby[begginer]
    ... public class Say { ... public static int say2{ ... parameter style java no sql language java ... type conversion for any parameters the method call may have. ...
    (comp.lang.java.databases)
  • function in derby[beginner]
    ... public class Say { ... public static int say2{ ... parameter style java no sql language java ... type conversion for any parameters the method call may have. ...
    (comp.lang.java.programmer)
  • function in derby[beginner]
    ... public class Say { ... public static int say2{ ... parameter style java no sql language java ... type conversion for any parameters the method call may have. ...
    (comp.lang.java.softwaretools)