如何在Default.rd.xml文件中定义数组

时间:2016-09-14 07:36:31

标签: c# arrays xml datacontractserializer

我正在发布模式下测试应用,但在使用InvalidDataContractException序列化我的数据时引用SerializationCodeIsMissingForType会失败。

通过消除过程,在Release(非Debug)模式下序列化失败的对象是Row [],Column [],它们只是简单类的数组。

我设法通过添加Default.rd.xml条目来序列化我的其他对象,如:

<Namespace Name="Windows.UI">
  <TypeInstantiation Name="Color" Arguments="System.Byte, System.Byte, System.Byte, System.Byte" Dynamic="Required All" DataContractSerializer="Required All"/>
</Namespace>

我很难尝试为System.Array提供正确的TypeInstantiation条目,因为我不知道如何使用强制Arguments参数。

任何人都可以帮助我,还是我完全走错了路?

...谢谢

罗伯特

1 个答案:

答案 0 :(得分:0)

我设法通过大量试验和错误使其工作。显然,所有内容都是在调试模式下构建和测试的,因此有一个序列化的对象列表,使用SerializationCodeIsMissingForType的发布版本失败。

为了帮助他人(因为还没有太多的例子)这是我的旅程。

我的序列化代码如下:

persistantData = new Dictionary<string, object>();

            persistantData.Add(nameof(Version), Version);
            persistantData.Add(nameof(FileVersion), FileVersion);
            persistantData.Add(nameof(ScrollParameters), ScrollParameters);
            persistantData.Add(nameof(DefaultCellBackgroundColour), DefaultCellBackgroundColour);
            persistantData.Add(nameof(Columns), Columns);
            persistantData.Add(nameof(Rows), Rows);
            persistantData.Add(nameof(Cells), listOfCellData);
            persistantData.Add(nameof(Filename), FileName);

private async Task SaveTheFileAsync(StorageFile file)
{
    using (MemoryStream memoryStream = new MemoryStream())
    {
    var serializer = new DataContractSerializer(typeof(Dictionary<string, object>));
    serializer.WriteObject(memoryStream, persistantData); //Failed here

...等

首先,我注释掉了上面的所有.Add(),但第一个除外。在我将以下内容添加到项目属性中的标准Default.rd.xml文件以定义字典集合类型之前,发生了相同的SerializationCodeIsMissingForType异常:

<Namespace Name="System.Collections.Generic">
  <TypeInstantiation Name="Dictionary" Arguments="System.String, System.Object" Dynamic="Required All" />
</Namespace>

添加Version对象需要将其添加到Default.rd.xml(Version的类型为PackageVersion)。

<Namespace Name="Windows.ApplicationModel">
      <TypeInstantiation Name="PackageVersion" Arguments="System.UInt16, System.UInt16, System.UInt16, System.UInt16" Dynamic="Required All" DataContractSerializer="Required All" />      
</Namespace>

添加FileVersion立即工作,因为它的类型是int。同样是ScrollParameters,因为它是一个包含两个双精度和浮点数的类。

DefaultCellBackgroundColour在Default.rd.xml中需要它:

<Namespace Name="Windows.UI">
  <TypeInstantiation Name="Color" Arguments="System.Byte, System.Byte, System.Byte, System.Byte" Dynamic="Required All" DataContractSerializer="Required All"/>
</Namespace>

Columns,Rows和Cells在Default.rd.xml中需要单独的Type条目:

<Type Name="Columns" Dynamic="Required All" DataContractSerializer="All" />
<Type Name="Rows" Dynamic="Required All" DataContractSerializer="All" />
<Type Name="CellData" Dynamic="Required All" DataContractSerializer="All" />

但是因为CellData是一个List集合,需要将类型添加到System.Collections.Generic命名空间条目(在Dictionary之后),如下所示:

<Namespace Name="System.Collections.Generic">
  <TypeInstantiation Name="Dictionary" Arguments="System.String, System.Object" Dynamic="Required All" DataContractSerializer="Required All" />
  <TypeInstantiation Name="List" Arguments="CellData" Dynamic="Required All" DataContractSerializer="All" />
</Namespace>

CellData定义主要包含字符串,整数,布尔值和小数,但一个属性定义为TextAlignment。后者在Default.rd.xml中需要自己的条目,如下所示:

<Namespace Name="Windows.UI.Xaml">
  <TypeInstantiation Name="TextAlignment" Arguments="System.Enum" Dynamic="Required All" DataContractJsonSerializer="Required All"/>
</Namespace>

最后,FileName(一个字符串)不需要额外的Default.rd.xml条目,而且一切正常。

感谢rbr94尝试提供帮助。此问题的标题现在具有误导性,因为不需要数组定义。

我的完整Default.rd.xml文件位于

之下

希望这有时可以帮助别人......

罗伯特

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
  <Application>
    <!--
      An Assembly element with Name="*Application*" applies to all assemblies in
      the application package. The asterisks are not wildcards.
    -->
    <Assembly Name="*Application*" Dynamic="Required All" />

    <!-- Add your application specific runtime directives here. -->

    <!-- Make all members of a type visible to .NET Native -->
    <Type Name="Columns" Dynamic="Required All" DataContractSerializer="All" />
    <Type Name="Rows" Dynamic="Required All" DataContractSerializer="All" />
    <Type Name="CellData" Dynamic="Required All" DataContractSerializer="All" />

    <Namespace Name="System.Collections.Generic">
      <TypeInstantiation Name="Dictionary" Arguments="System.String, System.Object" Dynamic="Required All" DataContractSerializer="Required All" />
      <TypeInstantiation Name="List" Arguments="CellData" Dynamic="Required All" DataContractSerializer="All" />
    </Namespace>

    <Namespace Name="Windows.ApplicationModel">
          <TypeInstantiation Name="PackageVersion" Arguments="System.UInt16, System.UInt16, System.UInt16, System.UInt16" Dynamic="Required All" DataContractSerializer="Required All" />      
    </Namespace>

    <Namespace Name="Windows.UI">
      <TypeInstantiation Name="Color" Arguments="System.Byte, System.Byte, System.Byte, System.Byte" Dynamic="Required All" DataContractSerializer="Required All"/>
    </Namespace>

    <Namespace Name="Windows.UI.Xaml">
      <TypeInstantiation Name="TextAlignment" Arguments="System.Enum" Dynamic="Required All" DataContractJsonSerializer="Required All"/>
    </Namespace>


  </Application>
</Directives>