How do I deserialize into an existing object - C#

后端 未结 4 1805
礼貌的吻别
礼貌的吻别 2020-12-18 23:42

In C#, after serializing an object to a file how would I deserialize the file back into an existing object without creating a new object?

All the examples I can fin

相关标签:
4条回答
  • 2020-12-18 23:52

    No problem, just use 2 classes. In getObject method you get your existing object

    [Serializable]
    public class McRealObjectHelper : IObjectReference, ISerializable 
    {
        Object m_realObject;
        virtual object getObject(McObjectId id)
        {
            return id.GetObject();
        }
        public McRealObjectHelper(SerializationInfo info, StreamingContext context)
        {
            McObjectId id = (McObjectId)info.GetValue("ID", typeof(McObjectId));
            m_realObject = getObject(id);
            if(m_realObject == null)
                return;
            Type t = m_realObject.GetType();
            MemberInfo[] members = FormatterServices.GetSerializableMembers(t, context);
            List<MemberInfo> deserializeMembers = new List<MemberInfo>(members.Length);
            List<object> data = new List<object>(members.Length);
            foreach(MemberInfo mi in members)
            {
                Type dataType = null;
                if(mi.MemberType == MemberTypes.Field)
                {
                    FieldInfo fi = mi as FieldInfo;
                    dataType = fi.FieldType;
                } else if(mi.MemberType == MemberTypes.Property){
                    PropertyInfo pi = mi as PropertyInfo;
                    dataType = pi.PropertyType;
                }
                try
                {
                    if(dataType != null){
                        data.Add(info.GetValue(mi.Name, dataType));
                        deserializeMembers.Add(mi);
                    }
                }
                catch (SerializationException)
                {
                    //some fiels are missing, new version, skip this fields
                }
            }
            FormatterServices.PopulateObjectMembers(m_realObject, deserializeMembers.ToArray(), data.ToArray());
        }
    
        public object GetRealObject( StreamingContext context )
        {
            return m_realObject;
        }
        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
        }
    }
    
    public class McRealObjectBinder: SerializationBinder
    {
        String assemVer;
        String typeVer;
        public McRealObjectBinder(String asmName, String typeName)
        {
            assemVer = asmName;
            typeVer = typeName;
        }
        public override Type BindToType( String assemblyName, String typeName ) 
        {
            Type typeToDeserialize = null;
            if ( assemblyName.Equals( assemVer ) && typeName.Equals( typeVer ) )
            {
                return typeof(McRealObjectHelper);
            }
            typeToDeserialize = Type.GetType( String.Format(  "{0}, {1}", typeName, assemblyName ) );
            return typeToDeserialize;
        }
    }
    

    Then, when deserialize:

    BinaryFormatter bf = new BinaryFormatter(null, context);
    bf.Binder = new McRealObjectBinder(YourType.Assembly.FullName, YourType.FullName);
    bf.Deserialize(memStream);
    
    0 讨论(0)
  • 2020-12-18 23:52

    If it's just a matter of copying a few fields, I would avoid all the trouble and take the simple route - deserialize into a new instance, then copy the appropriate fields to the existing instance. It will cost you a couple of extra copies, but you'll save a lot of time on debugging.

    0 讨论(0)
  • 2020-12-18 23:53

    Some serializers support callbacks; for example, both BinaryFormatter and DataContractSerializer (and protobuf-net, below) allow you to specify a before-serializaton callback, and since they skip the constructor, this may well be enough to initialize the object. The serializer is still creating it, though.


    Most serializers are fussy about wanting to create the new object themselves, however some will allow you to deserialize into an existing object. Well, actually the only one that leaps to mind is protobuf-net (disclosure: I'm the author)...

    This has 2 different features that might help here; for the root object (i.e. the outermost object in a graph) you can supply the existing object directly to either the Merge methods (in v1, also present in v2 for compatibility), or (in v2) the Deserialize methods; for example:

    var obj = Serializer.Merge<YourType>(source, instance);
    

    However, in a larger graph, you might want to supply other objects yourself (than just the root). The following is not exposed on the attribute API, but is a new feature in v2:

    RuntimeTypeModel.Default[typeof(SomeType)].SetFactory(factoryMethod);
    

    where factoryMethod can be either the name of a static method in SomeType (that returns a SomeType instance), or can be a MethodInfo to any static method anywhere. The method can additionally (optionally) take the serialization-context as a parameter if you want. This method should then be used to supply all new instances of SomeType.


    Note: protobuf-net is not quite the same as BinaryFormatter; for best effect, you need to tell it how to map your members - very similar to marking things as [DataMember] for WCF/DataContractSerializer. This can be attributes, but does not need to be.

    0 讨论(0)
  • 2020-12-19 00:00

    It's a bit unusual but it works:

    [Serializable]
    public class Pets
    {
        public int Cats { get; set; }
        public int Dogs { get; set; }
    }
    
    public static class Utils
    {
        public static byte[] BinarySerialize(object o)
        {
            using (var ms = new MemoryStream())
            {
                IFormatter f = new BinaryFormatter();
                f.Serialize(ms, o);
                return ms.ToArray();
            }
        }
    
        public static dynamic BinaryDeserialize(byte[] bytes, dynamic o)
        {
            using (var ms = new MemoryStream(bytes))
            {
                ms.Position = 0;
                IFormatter f = new BinaryFormatter();
                o = (dynamic)f.Deserialize(ms);
                return o;
            }
        }
    }
    

    The deserialization is tricky with the dynamic which cannot be passed by reference. And finally a no-sense test for proof:

    Pets p = new Pets() { Cats = 0, Dogs = 3 };
    Console.WriteLine("{0}, {1}", p.Cats, p.Dogs);
    byte[] serial = Utils.BinarySerialize(p);
    p.Cats = 1;
    Console.WriteLine("{0}, {1}", p.Cats, p.Dogs);
    p = Utils.BinaryDeserialize(serial, p);
    Console.WriteLine("{0}, {1}", p.Cats, p.Dogs);
    

    The output is the following:

    0, 3
    1, 3
    0, 3
    

    Replace memory strings with file stream and you will get the same results.

    0 讨论(0)
提交回复
热议问题