1

I wrote some abstract class that support quick serialize and deserialize. I add StructLayout to the implementation class and i got crash ( without the StructLayout its working with no crash )

I have 2 question

  1. Is there is any better fest way to do the Serialize ( on this i do serialization in TotalMilliseconds = 0.4834 and using the standard way of .net i takes TotalMilliseconds = 2.120 ) ?
  2. Why the 'StructLayout' crash my code ( exception on the runtime at the start of the application running

{"Could not load type 'WindowsFormsApplication4.Element1' from assembly 'WindowsFormsApplication4, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because the format is invalid.":"WindowsFormsApplication4.Element1"}

The code

public abstract class SerializeElement
{
    public abstract void Serialize(BinaryWriter bw);
    public abstract void DeSerialize(BinaryReader br);

    public sealed byte[] Serialize()
    {
        byte[] retVal = null;

        using(MemoryStream ms = new MemoryStream())
        {
            using(BinaryWriter bw = new BinaryWriter(ms))
            {
                Serialize(bw);
                retVal = ms.GetBuffer();
            }
        }

        return retVal;
    }

    public sealed void DeSerialize()
    {
        byte[] retVal = null;

        using(MemoryStream ms = new MemoryStream())
        {
            using(BinaryReader br = new BinaryReader(ms))
            {
                DeSerialize(br); 
            }
        }
    }
}


[StructLayout(LayoutKind.Sequential, Pack=4)]
public class Element1 : SerializeElement 
{
    public int Var1 { get; set; }
    public int Var2 { get; set; }

    public override void Serialize(BinaryWriter bw)
    {
        bw.Write(Var1);
        bw.Write(Var2);
    }

    public override void DeSerialize(BinaryReader br)
    {
        Var1 = br.ReadInt32();
        Var2 = br.ReadInt32();
    }
}


    static void Main()
    {
        Element1 e = new Element1()
        {
            Var1 = 1,
            Var2 = 2
        };

        var tt = e.Serialize();
Yanshof
  • 9,659
  • 21
  • 95
  • 195

2 Answers2

1

OK, after a little testing, I found out that .Net doesn't like the fact that Element1 inherits from SerializeElement while having the StructLayout attribute applied.

When I commented out inheriting from SerializeElement, the program ran fine. So, I suggest that you create a Data Transfer Object that encapsulates the properties used by Element1 and uses StructLayout attribute. Then, provide a conversion to a class that inherits SerializeElement so that you can serialize and deserialize that value.

You will need to make sure that the output is formatted correctly, but that shouldn't be too much of a problem.

In terms of BinaryWriter being the fastest solution, I would assume so. BinaryWriter is a simple abstraction over a Stream that just provides utilities for converting values to/from int/float/double/etc. values. But, the important part is knowing when you need the fastest solution. Time and time again, we(programmers) find out that sometimes the fastest solution isn't the best one. Often, the trade off of speed for development pace and quality is worth it.

With that said, I would recommend (as would others) that you try using protobuf-net. It is quite fast, and (in my opinion) the benefits of a general purpose serializer far outweigh having to write custom serializers for every single one of your classes.

Community
  • 1
  • 1
AtinSkrita
  • 1,373
  • 12
  • 13
  • 10x - i know about the StructLayout and the inherits .. but still looking the reason. using objectPool with those element give on my case the best way to do it ( i get network package each 20 millisecond ) – Yanshof Jun 21 '15 at 19:46
  • @Yanshof Yeah, I don't think many people know the answer to why StructLayout is killing the app. I have a feeling that the problem has to do with StructLayout-related code that gets run when `Element1` gets loaded, but there's not much of a way to know apart from looking at some of the decompiled code. – AtinSkrita Jun 21 '15 at 19:54
1

Sometimes the answer is just a click away ... the StructLayout definition don't give the inherited option

enter image description here

Yanshof
  • 9,659
  • 21
  • 95
  • 195