无法序列成员....因为它是一个接口 [英] Cannot serialize member.... because it is an interface

查看:308
本文介绍了无法序列成员....因为它是一个接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直有这个问题,并已拉出我的头发了它。我有跟随着错误:


  

异常详细信息:System.NotSupportedException:无法序列类型System.Collections.Generic.IList`1 [HannaPrintsDataAccess.CustomerAddress,HannaPrintsDataAccess成员HannaPrintsDataAccess.Customer.CustomerAddresses,版本= 1.0.0.0,文化=中立,公钥=空],因为它是一个接口


  
  

源错误:


  
  

196行:客户的客户= OperationsManager.Instance.CustomerService.GetCustomer(7);
  197线:
  198线:字符串XML = OperationsManager.Instance.CustomerService.GetCustomerAddressesXml(CustomerAddress.FindAll());
  199线:
  200线:订购订购= OperationsManager.Instance.OrderService.CreateOrderFromCart(XML);


  
  

源文件: c:\\HostingSpaces\\greetwus\\galadavetiye.com\\wwwroot\\HannaPrints\\HannaPrints\\WebUI\\CreateGreetingCard.aspx.cs行:198


  
  

堆栈跟踪:


  
  

[NotSupportedException异常:无法序列类型System.Collections.Generic.IList`1 [HannaPrintsDataAccess.CustomerAddress,HannaPrintsDataAccess成员HannaPrintsDataAccess.Customer.CustomerAddresses,版本= 1.0.0.0,文化=中立,公钥=空],因为它是一个接口。]


  
  

[出现InvalidOperationException:无法序列类型System.Collections.Generic.IList`1 [HannaPrintsDataAccess.CustomerAddress,HannaPrintsDataAccess,版本= 1.0.0.0的成员HannaPrintsDataAccess.Customer.CustomerAddresses',文化=中立,公钥=空] ]',请参见内部异常的更多细节。]
     System.Xml.Serialization.StructModel.CheckSupportedMember(TYPEDESC TYPEDESC,成员的MemberInfo,型型)889917
     System.Xml.Serialization.StructModel.GetPropertyModel(的PropertyInfo的PropertyInfo)+132 ........


我已经改变了我所有的IList对列出的,看看是否会做任何事情,但它没有,事实上,它没有更兼第二使得这些变化之后加载,即时猜测是因为错误发生它到达,即使之前部分。我检查了我的远程文件,看它是否被正确上传,这是

下面是code:

 使用系统;
使用System.Collections.Generic;
使用Castle.ActiveRecord;
命名空间HannaPrintsDataAccess {
    公共部分类客户{
        私人的IList _customerAddresses;
        公共CustomerAddress GetPrimaryCustomerAddress()
        {
            的foreach(在_customerAddresses CustomerAddress地址)
            {
                如果(address.IsPrimary)
                    退货地址;
            }
            返回null;
        }
        [的hasMany(typeof运算(CustomerAddress),ColumnKey =客户编号,表=客户)]
        公共虚拟的IList< CustomerAddress> CustomerAddresses
        {
            得到
            {
                返回this._customerAddresses;
            }
            组
            {
                this._customerAddresses =价值;
            }
        }
    }
}

在此code被激活发生错误:

 保护无效orderButton_Click(对象发件人,EventArgs的发送)
{
    客户客户= OperationsManager.Instance.CustomerService.GetCustomer(7);    字符串XML = OperationsManager.Instance.CustomerService.GetCustomerAddressesXml(CustomerAddress.FindAll());    订购订购= OperationsManager.Instance.OrderService.CreateOrderFromCart(XML);
    OperationsManager.Instance.CartService.MoveCart(MyDesigns);    的Response.Redirect(〜/客户/ PayByCreditCard.aspx orderGuid =?+ order.OrderGuid);
}

在CustomerAddress类:

 使用System.IO;
使用的System.Xml.Serialization;
使用Castle.ActiveRecord;
命名空间HannaPrintsDataAccess
{
公共部分类CustomerAddress
{
    公共toxml用于字符串()
    {
        XmlSerializer的序列化=新的XmlSerializer(的GetType());
        MemoryStream的MemoryStream的=新的MemoryStream();
        serializer.Serialize(MemoryStream的,这一点);
        memoryStream.Seek(0,SeekOrigin.Begin);
        返回新的StreamReader(的MemoryStream).ReadToEnd();
    }    [的belongsTo(客户编号)]
    公共虚拟客户客户{搞定;组; }
}
}


解决方案

在code你贴的类型, CustomerAddresses IList的< CustomerAdress> 。这是一个接口。像错误消息指出,你不能序列化的界面。

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

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

解决方案

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.

这篇关于无法序列成员....因为它是一个接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆