WCF can't serialize an interface either; in fact, it's impossible to serialize an interface over SOAP.
The reason (simplified) is that, when deserializing, .NET has to be able to create some actual concrete class. An interface is an abstract concept; there always has to be a "real" class implementation behind it in order for an actual instance to exist.
Since you can't construct a physical instance of an interface, it also can't be serialized.
If you're trying to use the XmlIgnoreAttribute
, understand that applying it to the type won't accomplish anything. It needs to be applied to the member instead. In other words:
public class SerializableClass
{
[XmlElement]
public int ID { get; set; }
[XmlElement]
public string Name { get; set; }
[XmlIgnore]
public IMyInterface Intf { get; set; }
}
...will serialize OK, because the serializer won't try to serialize the Intf
property. You just can't add the [XmlIgnore]
attribute to the IMyInterface
type definition (it won't compile).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…