C# 使用SharpSerializer反序列化异常

C# 使用SharpSerializer反序列化异常,c#,xaml,windows-phone-8,deserialization,binary-deserialization,C#,Xaml,Windows Phone 8,Deserialization,Binary Deserialization,我正在尝试二进制序列化来处理一些复杂的对象。 使用SharpSerializer,序列化工作没有问题,但我无法反序列化 下面是代码片段和堆栈跟踪 var settings = new SharpSerializerBinarySettings(); settings.IncludeAssemblyVersionInTypeName = true; settings.IncludeCultureInTypeName = true; settings.IncludePublicKeyTokenInT

我正在尝试二进制序列化来处理一些复杂的对象。 使用SharpSerializer,序列化工作没有问题,但我无法反序列化

下面是代码片段和堆栈跟踪

var settings = new SharpSerializerBinarySettings();
settings.IncludeAssemblyVersionInTypeName = true;
settings.IncludeCultureInTypeName = true;
settings.IncludePublicKeyTokenInTypeName = true;
settings.Mode = BinarySerializationMode.Burst;
var serializer = new SharpSerializer(settings);
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(basePath + "current" + extension, FileMode.Open, isoStore))
{    
    try
    {
        result = serializer.Deserialize(isoStream) as MyObject;
        System.Diagnostics.Debug.WriteLine("loaded from " + basePath + "current" + extension);
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine(ex.Message);
     }
 }
堆栈:

  ex {Polenter.Serialization.Core.DeserializingException: An error occured during the deserialization.
   Details are in the inner exception. ---> System.TypeLoadException: Could not load type 'otContent ShowGridLines BackgroundColorARGBOpacity TransformMatrixM11M12M21M22Offs' from assembly 'Polenter.SharpSerializer.Silverlight, Version=2.18.0.0, Culture=neutral, PublicKeyToken=8f4f20011571ee5f'.
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type)
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName)
   at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
   at System.Type.GetType(String typeName, Boolean throwOnError)
   at Polenter.Serialization.Advanced.TypeNameConverter.ConvertToType(String typeName)
   at Polenter.Serialization.Advanced.BurstBinaryReader.ReadType()
   at Polenter.Serialization.Advanced.BinaryPropertyDeserializer.deserialize(Byte elementId, String propertyName, Type expectedType)
   at Polenter.Serialization.Advanced.BinaryPropertyDeserializer.deserialize(Byte elementId, Type expectedType)
   at Polenter.Serialization.Advanced.BinaryPropertyDeserializer.Deserialize()
   at Polenter.Serialization.SharpSerializer.Deserialize(Stream stream)
   --- End of inner exception stack trace ---
   at Polenter.Serialization.SharpSerializer.Deserialize(Stream stream)
   at MyNamespace.DataManager.MyMethod()}   System.Exception {Polenter.Serialization.Core.DeserializingException}
我想反序列化一个复杂的结构,其中有一个自定义控件的层次结构和大量的继承

这个代码怎么了

编辑: 序列化代码

var settings = new SharpSerializerBinarySettings();
settings.IncludeAssemblyVersionInTypeName = true;
settings.IncludeCultureInTypeName = true;
settings.IncludePublicKeyTokenInTypeName = true;
settings.Mode = BinarySerializationMode.Burst;
var serializer = new SharpSerializer(settings);   
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(basePath + "current" + extension, FileMode.Create, isoStore))
{  
    serializer.Serialize(MyObject, isoStream);
    IsolatedStorageSettings.ApplicationSettings["saved"] = true;
    IsolatedStorageSettings.ApplicationSettings.Save();
    System.Diagnostics.Debug.WriteLine("Saved to " + basePath + "current" + extension);
}
我尝试序列化/反序列化的对象如下所示:

[DataContract]
public partial class MainObject : UserControl
{
    [IgnoreDataMember]
    private int zIndex = 10;

    [DataMember]
    private Dictionary<BasePlugin, Point> usedPlugins = new Dictionary<BasePlugin, Point>();

    ...methods...
}
作为补充信息,运行与昨天相同的代码会产生以下消息:


您手头有一个非常复杂的对象,需要(反)序列化。从您的角度来看,这似乎很简单,但您继承了一个已经很复杂的对象(
UserControl
)。默认情况下,SharpSerializer将序列化所有公共属性,不管发生什么。这很可能导致你的问题

可能的解决办法:

  • 尝试(反)序列化仅对仅用于序列化的对象中所需的信息/属性进行序列化。然后编写一些工具,在序列化对象和业务对象(您在这里介绍的对象)之间进行转换
  • 请尝试进一步配置SharpSeralizer,以便只存储所需的属性(也可以正确序列化)以及开箱即用的属性

能否提供要序列化的示例对象?或有关该类的更多信息。是否确实对序列化和反序列化使用了完全相同的配置?也许您还可以包含序列化代码。顺便问一下,我已经添加了一些代码:SharpSerializer是否支持
DataMember
属性?您知道SharpSerializer本身有很多选项和属性,可以让它按照您想要的方式工作吗?
/// <summary>
/// Base class from which every plugin derives
/// </summary>
[DataContract]
public abstract partial class BasePlugin: UserControl
{

    [IgnoreDataMember]
    protected StackPanel settingsPanel;
    public StackPanel SettingsPanel
    {
        get
        {                
            if (settingsPanel == null)
            {
                settingsPanel = buildSettingsPanel();
            }
            return settingsPanel;
        }
    }

    [DataMember]
    protected Color backgroundColor = Color.FromArgb(255, 255, 255, 255);
    public Color BackgroundColor
    {
        get { return backgroundColor; }
        set { backgroundColor = value; }
    }

    [DataMember]
    protected Color foregroundColor = Color.FromArgb(255, 0, 0, 0);
    public Color ForegroundColor
    {
        get { return foregroundColor; }
        set { foregroundColor = value; }
    }

    [DataMember]
    protected string pluginName;
    public string PluginName
    {
        get { return pluginName; }
        protected set { pluginName = value; }
    }

    [DataMember]
    protected string pluginDescription;
    public string PluginDescription
    {
        get { return pluginDescription; }
        protected set { pluginDescription = value; }
    }

    [DataMember]
    protected Category pluginCategory;
    public Category PluginCategory
    {
        get { return pluginCategory; }
        protected set { pluginCategory = value; }
    }

    ...methods...
}
  Message "Error during creating an object. Please check if the type \"System.Windows.Markup.XmlLanguage, System.Windows, Version=2.0.6.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e\" has public parameterless constructor, or if the settings IncludeAssemblyVersionInTypeName, IncludeCultureInTypeName, IncludePublicKeyTokenInTypeName are set to true. Details are in the inner exception."  string