【问题标题】:SubSonic - Serializing Classes with Nullable types and collections?SubSonic - 使用可空类型和集合序列化类?
【发布时间】:2009-12-10 01:14:09
【问题描述】:

使用 SubSonic v2.x:第一个问题是讨论的错误here

“/......”应用程序中的服务器错误。 无法序列化 System.Nullable 类型的成员“.....”

我不确定将这篇文章中的代码放在我的 DAL 中的哪个位置以使其正常工作。我尝试将它放在我为表创建的部分类中,但不行。

另一个解决方法是添加:

generateNullableProperties="false"

但是,在我的 DAL 的亚音速配置的供应商部分的智能感知中,这不是一个选项。 (它应该在 DAL 配置或应用程序的配置上进行?)

我设法解决了这个问题

[XmlElement(Type = typeof(TblReceiptLineItem))]

转至以下代码:

public partial class TblReceipt
{
    // no need to override any existing methods or properties as we 
    // are simply adding one

    //[XmlElement]

    // specifying the types of objects to be contained within the arraylist
    [XmlElement(Type = typeof(TblReceiptLineItem))]
    public ArrayList ReceiptLineItemsArr = new ArrayList();
    public string UserPhoneNumber;
    public string UserCardNumber;
}

...只是因为我将 TblReceiptLineItem 中的可为空字段更改为不可为空。

但是,现在的错误是:

无法将“System.Collections.ArrayList”类型的对象转换为“DAL.TblReceiptLineItem”类型。

所以我认为它甚至还没有达到 Nullable 类型错误并且不喜欢演员表。

那么,什么是序列化对象的最佳方式,该对象包含一个具有 1..* 自定义类型元素的集合(即 SubSonic 友好)。 反序列化这些数据的最佳方法是什么?

第二个(还没有)问题是我有一个对象,其中一个成员中有一组对象。我是否必须在序列化整个对象之前序列化对象内的集合,还是 XmlSerializer 会处理所有这些?

谢谢。

== 更新 ==

所以看起来问题实际上已经通过我上面的小修复解决了。我有一些其他错误代码导致了第二个错误。

但是,我的代码现在完全序列化了主对象,其中包含对象的数组列表。

所以解决方法是在声明之前添加类型:

[XmlElement(Type = typeof(TblReceiptLineItem))]

但是欢迎使用更好的方法来完成这个(或其他)。

【问题讨论】:

    标签: .net subsonic xml-serialization nullable


    【解决方案1】:

    问题 1 - 我不知道

    问题 2 - 这取决于集合和对象。一些集合(前字典)是不可序列化的。如果集合是可序列化的,那么是的,整个对象图都应该被序列化。当然,延迟加载也可能会造成麻烦。

    编辑: 有什么理由不适合您?

        [Serializable]
        public partial class tblReciept
        {
            public List<TblReceiptLineItem> ReceiptLineItemsArr { get; set; }
        }
    
        [Serializable]
        public class TblReceiptLineItem
        {
            public int ItemId { get; set; }
        }
    
        class Program
        {
            static void Main( string[] args )
            {
                var reciept = new tblReciept
                {
                    ReceiptLineItemsArr = new List<TblReceiptLineItem>
                    {
                        new TblReceiptLineItem { ItemId = 1 },
                        new TblReceiptLineItem { ItemId = 222 },
                        new TblReceiptLineItem { ItemId = 156 }
                    }
                };
    
                XmlSerializer s = new XmlSerializer( typeof( tblReciept ) );
                TextWriter w = new StreamWriter( @"c:\list.xml" );
                s.Serialize( w, reciept );
                w.Close( );
            }
    
        }
    
    
    // output 
    <?xml version="1.0" encoding="utf-8"?>
    <tblReciept xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <ReceiptLineItemsArr>
        <TblReceiptLineItem>
          <ItemId>1</ItemId>
        </TblReceiptLineItem>
        <TblReceiptLineItem>
          <ItemId>222</ItemId>
        </TblReceiptLineItem>
        <TblReceiptLineItem>
          <ItemId>156</ItemId>
        </TblReceiptLineItem>
      </ReceiptLineItemsArr>
    </tblReciept>
    

    【讨论】:

    • 在我的表的部分类中,我添加了一个 ArrayList 属性。但是,您要说的是,在序列化的时候这将不起作用。所以试图使它成为一个数组 - 我收到这个错误:“无法创建抽象类或接口 System.Array 的实例”。
    • ... 但是,分配 XmlElementAttribute 的实例应该注意这一点,不是吗?: [XmlElement(Type = typeof(TblReceiptLineItem))] - 在 ArrayList 声明之前...
    【解决方案2】:

    很高兴您已修复它 - 序列化可空类型通常在 C# 2.0 中崩溃和烧毁 - 但如果它有效,万岁!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      • 2011-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多