Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
626 views
in Technique[技术] by (71.8m points)

c# - Cannot serialize member.... because it is an interface

I have been having this problem and been pulling my hair out over it. I have the followin error:

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:HostingSpacesgreetwusgaladavetiye.comwwwrootHannaPrintsHannaPrintsWebUICreateGreetingCard.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........

I have changed all my IList's to List's to see if that would do anything, but it didnt, infact, it didnt even take a second to load after making those changes, im guessing because the error happens even before it gets to that part. I checked my remote files to see if it was uploading correctly and it was.

Here is the code:

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

The error happens when this code is activated:

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

The CustomerAddress class:

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; }
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In the code you posted, the type of CustomerAddresses is IList<CustomerAdress>. That's an interface. Like the error message says, you can't serialize an interface.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...