2010-09-03 26 views
19

我一直有这个问题,一直拉着我的头发。我有跟随着错误:无法序列化成员....因为它是一个接口

Exception Details: System.NotSupportedException: Cannot serialize member HannaPrintsDataAccess.Customer.CustomerAddresses of type System.Collections.Generic.IList`1[[HannaPrintsDataAccess.CustomerAddress, HannaPrintsDataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] because it is an interface.

Source Error:

Line 196: Customer customer = OperationsManager.Instance.CustomerService.GetCustomer(7); Line 197: Line 198: string xml = OperationsManager.Instance.CustomerService.GetCustomerAddressesXml(CustomerAddress.FindAll()); Line 199: Line 200: Order order = OperationsManager.Instance.OrderService.CreateOrderFromCart(xml);

Source File: c:\HostingSpaces\greetwus\galadavetiye.com\wwwroot\HannaPrints\HannaPrints\WebUI\CreateGreetingCard.aspx.cs Line: 198

Stack Trace:

[NotSupportedException: Cannot serialize member HannaPrintsDataAccess.Customer.CustomerAddresses of type System.Collections.Generic.IList`1[[HannaPrintsDataAccess.CustomerAddress, HannaPrintsDataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] because it is an interface.]

[InvalidOperationException: Cannot serialize member 'HannaPrintsDataAccess.Customer.CustomerAddresses' of type 'System.Collections.Generic.IList`1[[HannaPrintsDataAccess.CustomerAddress, HannaPrintsDataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]', see inner exception for more details.] System.Xml.Serialization.StructModel.CheckSupportedMember(TypeDesc typeDesc, MemberInfo member, Type type) +889917 System.Xml.Serialization.StructModel.GetPropertyModel(PropertyInfo propertyInfo) +132........

我已经改变了我所有的IList的列出的,看看是否会做任何事情,但它没有,事实上,它甚至没有采取第二个做出这些修改后加载,即时猜测因为在它到达那个部分之前就会发生错误。我检查了我的远程文件,看它是否正确上传,它是。

下面是代码:

using System; 
using System.Collections.Generic; 
using Castle.ActiveRecord; 
namespace HannaPrintsDataAccess { 
    public partial class Customer { 
     private IList _customerAddresses; 


     public CustomerAddress GetPrimaryCustomerAddress() 
     { 
      foreach (CustomerAddress address in _customerAddresses) 
      { 
       if (address.IsPrimary) 
        return address; 
      } 
      return null; 
     } 


     [HasMany(typeof(CustomerAddress), ColumnKey = "CustomerId", Table = "Customer")] 
     public virtual IList<CustomerAddress> CustomerAddresses 
     { 
      get 
      { 
       return this._customerAddresses; 
      } 
      set 
      { 
       this._customerAddresses = value; 
      } 
     } 
    } 
} 

错误发生时,该代码被激活:

protected void orderButton_Click(object sender, EventArgs e) 
{ 
    Customer customer = OperationsManager.Instance.CustomerService.GetCustomer(7); 

    string xml = OperationsManager.Instance.CustomerService.GetCustomerAddressesXml(CustomerAddress.FindAll()); 

    Order order = OperationsManager.Instance.OrderService.CreateOrderFromCart(xml); 
    OperationsManager.Instance.CartService.MoveCart("MyDesigns"); 

    Response.Redirect("~/Customer/PayByCreditCard.aspx?orderGuid=" + order.OrderGuid); 
} 

的CustomerAddress类:

using System.IO; 
using System.Xml.Serialization; 
using Castle.ActiveRecord; 


namespace HannaPrintsDataAccess 
{ 
public partial class CustomerAddress 
{ 
    public string ToXml() 
    { 
     XmlSerializer serializer = new XmlSerializer(GetType()); 
     MemoryStream memoryStream = new MemoryStream(); 
     serializer.Serialize(memoryStream, this); 
     memoryStream.Seek(0, SeekOrigin.Begin); 
     return new StreamReader(memoryStream).ReadToEnd(); 
    } 

    [BelongsTo("CustomerId")] 
    public virtual Customer Customer { get; set; } 
} 
} 

回答

22

在您发布的代码中,CustomerAddresses的类型为IList<CustomerAdress>。这是一个界面。就像错误信息所说,你不能序列化一个接口。

+0

我明白,但我试图将其更改为普通列表,但它给了我同样的错误,并且确保代码已上传并正确更改 – anthonypliu 2010-09-03 04:07:09

+0

@anthony:不,您没有将其更改为列表并拥有它以同样的方式失败。对不起,你做错了什么。可能有些愚蠢的做法,就是将虚拟属性的返回值作为IList ',并从覆盖中返回列表'。 – 2010-09-03 04:21:43

+0

我遇到同样的问题。当我这样做时,“XmlSerializer serializer = new XmlSerializer(kevinObject)”,其中kevinObject是具有[DataContract]属性的对象。在kevinObject中,有一个名为“INewObject”的字段,因为有多个INewObject实现。有没有一种方法可以包含标有[DataContract]的界面? – 2012-01-18 15:08:05

-1

不是你的问题的根源,但你需要

using (MemoryStream memoryStream = new MemoryStream()) 
{ 
    serializer.Serialize(memoryStream, this); 
    memoryStream.Seek(0, SeekOrigin.Begin); 
    using (StreamReader reader = new StreamReader(memoryStream)) 
    { 
     return reader.ReadToEnd(); 
    } 
} 
+1

如果'serializer'是一个XmlSerializer,用户声明并且类型不能被序列化,那么这将如何工作? – 2012-07-05 07:47:59

+0

如果无法序列​​化类型,则不会有效。 – 2012-07-05 14:34:05

相关问题