0

我有 2 个派生自通用基类的类。

[Serializable()]
[XmlInclude(typeof(User))]
[XmlInclude(typeof(File))]
public class BaseEntity<T>
{
    private long id;

    [XmlAttribute(AttributeName = "id")]
    public virtual long ID
    {
        get { return this.id; }
        set { this.id = value; }
    }
}

[Serializable()]
public class User : BaseEntity<User>
{
    private string userName;
    private string sharedDirectory;
    private bool connected;

    [XmlAttribute(AttributeName = "UserName")]
    public virtual string UserName 
    {
        get { return this.userName; }
        set { this.userName = value; } 
    }

    [XmlAttribute(AttributeName = "SharedDirectory")]
    public virtual string SharedDirectory
    {
        get { return this.sharedDirectory; }
        set { this.sharedDirectory = value; }
    }

    [XmlAttribute(AttributeName = "IsConnected")]
    public virtual bool IsConnected
    {
        get { return this.connected; }
        set { this.connected = value; }
    }
}

[Serializable()]
public class File : BaseEntity<File>
{
    private string name;
    private User user;

    [XmlAttribute(AttributeName = "Name")]
    public virtual string Name
    {
        get { return this.name; }
        set { this.name = value; }
    }

    [XmlElement(ElementName = "User", Type = typeof(User))]
    public virtual User User
    {
        get { return this.user; }
        set { this.user = value; }
    }
}

我有一个返回文件列表(列表)的 Web 服务(不是 WCF 服务)。当我调用该服务时,该方法已执行,但序列化过程失败。这是我得到的例外:

System.Web.Services.Protocols.SoapException:服务器无法处理请求。---> System.InvalidOperationException:生成 XML 文档时出错。---> System.InvalidOperationException:类型 Castle.Proxies.UserProxy 不是预期的。使用 XmlInclude 或 SoapInclude 属性指定静态未知的类型。\n 在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write4_User(String n, String ns, User o, Boolean isNullable, Boolean needType)\n 在 Microsoft。 Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write6_File(String n, String ns, File o, Boolean isNullable, Boolean needType)\n 在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write7_GetFilesByNameResponse(Object[] p)\n 在 Microsoft。 Xml.Serialization.GeneratedAssembly。

有谁知道为什么?

4

1 回答 1

1

这是因为我使用了lazy=true,并且当我序列化我的对象时,序列化程序试图序列化不允许的代理对象。当我将映射更改为lazy=false时,它起作用了!

于 2012-08-05T20:10:30.203 回答