2010-09-03 34 views
19

Bu sorunu yaşıyorum ve saçlarımın üzerinden çekiyordum. Ben bütün ılist en o her şeyi yapacağını görmek için listesinde, ama buna bile bu değişiklikleri yaptıktan sonra yüklemek için ikinci sürmedi, gerçeği yoktu üzere, im tahmin değiştiÜye serileştirilemiyor ... çünkü bir arabirim

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

: Ben followin hata var çünkü hata o parçaya ulaşmadan önce gerçekleşir. Uzak dosyalarımı doğru yükleyip yüklemediğini kontrol ettim ve öyleydi. İşte

kodudur:

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

hata bu kod devreye girdiğinde gerçekleşir:

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 sınıfı:

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

cevap

22

Gönderdiğiniz kodda, CustomerAddresses türü IList<CustomerAdress> şeklindedir. Bu bir arayüz. Hata mesajında ​​olduğu gibi, bir arayüzü seri hale getiremezsiniz.

+0

Bunu anlıyorum, ancak bunu normal Listeye değiştirmeyi denedim, ancak bana SAME hatasını verdi ve kodun düzgün şekilde yüklendiğinden ve düzgün şekilde değiştirildiğinden emin oldum – anthonypliu

+0

@anthony: no, onu bir listeye değiştirmediniz aynı şekilde başarısız. Üzgünüm, yanlış bir şey yaptın. Muhtemelen aptalca bir şey gibi sanal özellik dönüş değeri 'IList ' bırakın ve sadece geçersiz kılma 'List 'döndürün. –

+0

Aynı problemi yaşıyorum. Yaptığımda, "XmlSerializer serializer = yeni XmlSerializer (kevinObject)", burada kevinObject özniteliği olan bir nesnedir [DataContract]. KevinObject'te, "INewObject" adlı bir alan var çünkü INewObject'in birden çok uygulaması var. [DataContract] işaretli bir Arabirimi dahil etmenin bir yolu var mı? –

-1

Değil sorunun kaynağı, ama ihtiyacınız

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

Eğer "serializer", kullanıcının belirttiği gibi bir XmlSerializer oluşturulduysa ve tür serileştirilemiyorsa bu nasıl çalışır? –

+0

Tür, seri hale getirilemezse, hiçbir şey çalışmayacaktır. –