c# - ComponentSerializationService 不反序列化所有属性

标签 c#

我正在使用找到的 ComponentSerializationService here :

当您从设计服务中剪切控件时,将调用序列化方法,而当您粘贴反序列化时。

在我的例子中 - 我有一个简单的按钮,我已经改变了背景颜色。

序列化看起来像这样:

public object Serialize(ICollection objects)
      {
         var serializationService = _serviceProvider.GetService(typeof(ComponentSerializationService)) as ComponentSerializationService;

         if (serializationService == null)
         {
            throw new Exception("ComponentSerializationService not found");
         }

         SerializationStore returnObject;

         using (var serializationStore = serializationService.CreateStore())
         {
            foreach (object obj in objects)
            {
               if (obj is Control)
               {
                  serializationService.Serialize(serializationStore, obj);
               }                  
            }

            returnObject = serializationStore;
         }

         return returnObject;
      }

反序列化看起来像这样:

public ICollection Deserialize(object serializationData)
      {
         var serializationStore = serializationData as SerializationStore;

         if (serializationStore == null)
         {
            return new object[] {};
         }

         var componentSerializationService = _serviceProvider.GetService(typeof(ComponentSerializationService)) as ComponentSerializationService;

         if (componentSerializationService == null)
         {
            throw new Exception("ComponentSerializationService not found");
         }

         var collection = componentSerializationService.Deserialize(serializationStore);

         return collection;
      }

我在两个方法中都设置了断点,并且传入的对象包含正确的背景属性,但是一旦反序列化,该属性就不会保留。

欢迎任何想法。这是一个棘手的类,代码示例或文档行中很少。

最佳答案

你应该看看这个,看看 SerializeAbsolute 是否适合你:)

https://msdn.microsoft.com/en-us/library/system.componentmodel.design.serialization.componentserializationservice.serializeabsolute(v=vs.110).aspx

更具体地说,我引用了备注

Standard serialization, as implemented through the Serialize method, only serializes values that differ from the component's default state. This provides the most compact serialization mechanism but assumes that a newly created object will be used during deserialization. If an existing object is used, the resulting deserialized object is not guaranteed to duplicate the original state of the serialized object; the properties that contained default values during serialization will not be reset back to their defaults during deserialization. The SerializeAbsolute method does not use this shortcut. It serializes all properties of the source object so that deserialization can restore all the object's properties, regardless of default state.

关于c# - ComponentSerializationService 不反序列化所有属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35722234/

相关文章:

c# - SQLite 中如何将事务隔离级别设置为 ReadUncommitted?

c# - 是否可以在 LINQ 中表达此代码?

c# - 使用 WiX 工具集编辑文件

c# - 在 ASP.Net c# 中执行多个读取器

c# - 还有另一种方法可以将 ffmpeg 中的帧导出到 texture2d 吗?我的代码在 Windows 中工作但在 Linux 中不工作

c# - 导出到 excel 的 RadGrid telerik 问题

c# - 在 .NET 应用程序中删除不必要的 namespace 的方法?

c# - 在C#中播放声音

c# - 定时器更新标签

c# - 检查列表中的任何项目是否与另一个列表中的任何项目匹配