Determining .NET Serialized Size and Unmanaged Memory Performance

My question is whether it is possible to determine the serialized size (in bytes) of the reference type.

Here is a situation:

I use the BinaryFormatter class to serialize the basic .NET types, for example, for example:

[Serializable]
public class Foo
{
    public string Foo1 { get; set; }
    public string Foo2 { get; set; } 
}

I serialize each element into byte [], then add this segment to the end of the existing byte [] and optionally add a carriage return at the end of each segment to delimit objects.

For deserialization, I use Marshal.ReadByte () as follows:

List<byte> buffer = new List<byte>();

for (int i = 0; i < MapSize; i++)
{
    byte b = Marshal.ReadByte(readPtr , i); 

    if (b != delim)  // read until encounter a carriage return 
        buffer.Add(b);
    else
        break;
}

readPtr = readPtr + buffer.Count + 1; // incrementing the pointer for the next object

return buffer.ToArray(); 

, Marshal.Copy() , . , , , , ?

, . , BinaryFormatter - ? , , BinaryFormatter , , < > ?

+3
3

. BinaryFormatter : http://msdn.microsoft.com/en-us/library/cc236844(v=prot.10).aspx

:

  • . . , .
  • . . - , .
  • . , ( , ).

-, , . . BinaryFormatter. . , BinaryFormatter .

, , . BinaryFormatter , .

, , " ". , .

+3

- - 13 - , , "".

.

+4

Marshal.SizeOf, . , StructLayout.

, , :

CLR . # . .

-blit , SequentialLayout. http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.structlayoutattribute.aspx , , . : " (AutoLayout, SequentialLayout ExplicitLayout) , ."

System.Reflection.TypeAttributes. CLR. # , ilasm.exe .

+2

All Articles