C#使用DataContractResolver将数据写入文件

时间:2017-03-03 09:33:37

标签: c# prism

我正在尝试使用此代码将数据写入文件;

   public async Task<bool> WriteDataAsync<T>(T data, string fileName, string folderPath)
    {
        try
        {
            var store = await GetFolder(folderPath);

            using (MemoryStream sessionData = new MemoryStream())
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof(T));
                serializer.WriteObject(sessionData, data);

                StorageFile sessionFile = await store.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);

                using (Stream fileStream = await sessionFile.OpenStreamForWriteAsync())
                {
                    sessionData.Seek(0, SeekOrigin.Begin);
                    await sessionData.CopyToAsync(fileStream);
                    await fileStream.FlushAsync();
                }
            }

        }

很长一段时间它工作正常,但后来我得到了这个例外;

键入&#39; Microsoft.Practices.Prism.PubSubEvents.EventAggregator&#39;使用数据合同名称&#39; EventAggregator:http://schemas.datacontract.org/2004/07/Microsoft.Practices.Prism.PubSubEvents&#39;不是预期的。如果您正在使用DataContractSerializer或者将任何静态未知的类型添加到已知类型列表中,请考虑使用DataContractResolver - 例如,使用KnownTypeAttribute属性或将它们添加到传递给序列化程序的已知类型列表中。

所以我查看了DataContractResolver,我在MSDN中找到了这个页面;

https://msdn.microsoft.com/en-us/library/ee358759(v=vs.110).aspx

我创建了修复错误的代码;

public class LogResolver : DataContractResolver
{
    public override bool TryResolveType(Type dataContractType, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
    {
        if (dataContractType != typeof(LogViewModel))
        {
            return knownTypeResolver.TryResolveType(dataContractType, declaredType, null, out typeName,
                out typeNamespace);
        }

        var dictionary = new XmlDictionary();
        typeName = dictionary.Add("Log");
        typeNamespace = dictionary.Add("http://tempuri.com");
        return true;
    }

    public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
    {
        if (typeName == "Log" && typeNamespace == "http://tempuri.com")
        {
            return typeof(LogViewModel);
        }

        return knownTypeResolver.ResolveName(typeName, typeNamespace, null, null);
    }
}  

DataContractSerializer serializer = new DataContractSerializer(typeof(T));
if (typeof(T).Name == "LogViewModel")
{
    var writer = XmlDictionaryWriter.CreateDictionaryWriter(XmlWriter.Create(sessionData));
    serializer.WriteObject(writer, data, new LogResolver());
}
else
{
    serializer.WriteObject(sessionData, data);
}

但现在我的代码无法编译,因为serializer.WriteObject方法不带3个参数。 怎么会这样?当然MSDN不能错?

EDIT 我决定查看WriteObject所在的序列化类的定义,我发现了这个;

// Decompiled with JetBrains decompiler
// Type: System.Runtime.Serialization.XmlObjectSerializer
// Assembly: System.Runtime.Serialization.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// MVID: 94FDD0A8-CB5D-4815-A53D-5DC4F6C5FA80
// Assembly location: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.6\Profile\Profile32\System.Runtime.Serialization.Xml.dll

using System.IO;
using System.Xml;

namespace System.Runtime.Serialization
{
  public abstract class XmlObjectSerializer
  {
    protected XmlObjectSerializer();
    public abstract bool IsStartObject(XmlDictionaryReader reader);
    public virtual bool IsStartObject(XmlReader reader);
    public virtual object ReadObject(Stream stream);
    public virtual object ReadObject(XmlDictionaryReader reader);
    public abstract object ReadObject(XmlDictionaryReader reader, bool verifyObjectName);
    public virtual object ReadObject(XmlReader reader);
    public virtual object ReadObject(XmlReader reader, bool verifyObjectName);
    public abstract void WriteEndObject(XmlDictionaryWriter writer);
    public virtual void WriteEndObject(XmlWriter writer);
    public virtual void WriteObject(Stream stream, object graph);
    public virtual void WriteObject(XmlDictionaryWriter writer, object graph);
    public virtual void WriteObject(XmlWriter writer, object graph);
    public abstract void WriteObjectContent(XmlDictionaryWriter writer, object graph);
    public virtual void WriteObjectContent(XmlWriter writer, object graph);
    public abstract void WriteStartObject(XmlDictionaryWriter writer, object graph);
    public virtual void WriteStartObject(XmlWriter writer, object graph);
  }
}

因此没有使用DataContractResolver的WriteObject方法。那我该怎么办?

编辑 - 解决方法而不是解决方案。 如果查看错误消息,问题是正在编写的对象包含无法序列化的事件。 此事件在所有ViewModel中使用的BaseViewModel类中定义。所以我创建了一个新的基类,只供那些需要它的ViewModel使用,如下所示; 名称空间M.Survey.UILogic.ViewModels {     使用M.Survey.UILogic.Helpers;     使用Microsoft.Practices.Prism.PubSubEvents;

public class BaseEventViewModel : BaseViewModel
{
    public BaseEventViewModel()
    {
        this.EventAgg = EventAggHolder.EventAgg;
    }

    public IEventAggregator EventAgg { get; set; }
}

所以我的问题现在已经解决了。但它留下了如何编写包含事件的对象的开放性。在WinRT中,我无法找到可以排除导致此错误的序列化事件的属性。

1 个答案:

答案 0 :(得分:1)

我怀疑WinRT不支持此 WriteObject 重载。您可以尝试以下替代方案。

  1. 使用 DataContractSerializerSettings 设置 DataContractResolver ,然后将设置对象传递给 DataContractSerializer 构造函数。然后可以照常使用 serializer.WriteObject(writer,data)

    DataContractSerializerSettings settings = new DataContractSerializerSettings();
    settings.DataContractResolver = new LogResolver();
    DataContractSerializer serializer = new DataContractSerializer(typeof(T), settings);
    
  2. 使用KnownType attibute指定序列化程序要检测的已知类型列表。

  3. 更多信息:https://social.msdn.microsoft.com/Forums/expression/en-US/9cea4fa4-7dde-46b9-9d51-40eca3d21a18/how-to-assing-knowntypeattribute-on-winrt?forum=winappswithcsharp

    希望这可以帮助您的方案!

    编辑:由于问题在于序列化了不需要的属性,因此使用IgnoreDataMember将指示序列化程序将其从序列化中排除。