2010-09-03 112 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

相關問題