我有 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。
有谁知道为什么?