如何在Default.rd.xml文件中定义Array ?

本文关键字:定义 Array 文件 xml Default rd | 更新日期: 2023-09-27 18:07:06

我正在测试一个应用程序在发布模式,它失败时序列化我的数据与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想出一个正确的TypeInstantiation条目。数组,因为我不知道该用什么作为强制参数

有人能帮我吗,还是我完全走错了路?

谢谢…罗伯特

如何在Default.rd.xml文件中定义Array ?

我经过反复试验才使它工作起来。显然,所有的东西都是在调试模式下构建和测试的,所以有一个序列化的对象列表,在使用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(),除了第一个。同样的SerializationCodeIsMissingForType异常会发生,直到我在项目的Properties中的标准Default.rd.xml文件中添加以下内容来定义Dictionary集合类型:

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

添加版本对象需要将此添加到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也是一样,因为它是一个包含两个double和一个float的类。

defaultcellbackgroundcolor需要在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条目:

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