leejw51 / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
0 stars 0 forks source link

When serializing deserializing an enum 0. element does not serialize if parameterless constructor also sets the same property. #251

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago

What steps will reproduce the problem?
- When serializing deserializing an enum 0. element does not serialize if 
parameterless constructor also sets the same property.
-  I have written a test project to illustrate the problem
i can set the initial message's constructor to Y and transforms correctly but 
if i use the 0. element the X protobuf does't set attribute after contstructor.

What is the expected output? What do you see instead?
Default instead of X

What version of the product are you using? On what operating system?
2.0.0.431 win7 ultimate

Please provide any additional information below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProtoBuf;
using System.Runtime.Serialization;

namespace Prototest
{
    class Program
    {
        static void Main(string[] args)
        {

            Message msgState = new Message(TestApplications.X);
            ProtoSerializationHelper helper = new ProtoSerializationHelper();
            byte[] bytes = helper.ObjectToByteArray(msgState);
            Message otherside = (Message)helper.ByteArrayToObject(bytes);
        }
    }
    public class ProtoSerializationHelper
    {
        #region Methods

        public object ByteArrayToObject(byte[] byteArray)
        {
            try
            {
                System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(byteArray);
                memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
                return Serializer.Deserialize<Message>(memoryStream);
            }
            catch (Exception exception)
            {
                // Error
                throw new Exception(String.Format("Exception caught in process: {0}", exception.ToString()), exception);
            }

        }

        public byte[] ObjectToByteArray(Object obj)
        {
            Message msg = obj as Message;
            try
            {
                System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
                Serializer.Serialize(memoryStream, msg);
                return memoryStream.ToArray();
            }
            catch (Exception exception)
            {
                // Error
                throw new Exception(String.Format("Exception caught in process: {0}", exception.ToString()), exception);
            }

        }

        #endregion
    }

    public enum TestApplications
    {
        X,
        Y,
        Z,
        Default,
    }

      [ProtoContract, Serializable]
    public class Message
    {
        #region Fields

        TestApplications application;

        #endregion

        #region Constructors

        public Message(TestApplications application)
        {
            Application = application;
        }

        public Message()
        {
            SetDefaultApp();
        }

        #endregion

        #region Methods

        private void SetDefaultApp()
        {
            Application = TestApplications.Default;
        }

        #endregion

        #region Properties

        [ProtoMember(1)]
        public TestApplications Application
        {
            get
            {
                return application;
            }
            set
            {
                application = value;
            }
        }

        #endregion

    }
}

Original issue reported on code.google.com by moko...@gmail.com on 18 Nov 2011 at 2:34