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

How do I define an Array in the Default.rd.xml file

我正在发布模式下测试一个应用程序,当使用 InvalidDataContractException 引用 SerializationCodeIsMissingForType 序列化我的数据时它失败了。

通过消除过程,在发布(而非调试)模式下序列化失败的对象是 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 参数使用什么。

谁能帮帮我,还是我完全走错了路?

谢谢...

罗伯特

我通过反复试验设法使它工作。显然,所有内容都已在调试模式下构建和测试,因此有一个正在序列化的 objects 列表未能使用 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()s 除了第一个。在我将以下内容添加到项目属性中的标准 Default.rd.xml 文件以定义字典 ​​collection 类型之前,发生了相同的 SerializationCodeIsMissingForType 异常:

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

添加版本 object 需要将其添加到 Default.rd.xml(版本类型为 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 因为它是一个 class 包含两个双打和一个浮点数。

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>

列、行和单元格需要 Default.rd.xml 中的单独类型条目:

<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 是一个列表 collection,所以需要将类型添加到 System.Collections.Generic 命名空间条目(在字典之后),如下所示:

<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>