I'm using a REST service that always returns an object of type ServiceResult. The cool thing about WCF is that the xml d开发者_高级运维eserialization is done automatically. Below are two example operation contracts:
[ServiceContract]
[XmlSerializerFormat]
public interface IMyService
{
[OperationContract]
[WebGet(
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Xml,
UriTemplate = "authenticate?api={apiKey}&userName={userName}&password={password}")]
ServiceResult Authenticate(string apiKey, string userName, string password);
[OperationContract]
[WebGet(
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Xml,
UriTemplate = "getSchedules?api={apiKey}&user={userAccessCode}&states={states}&types={types}&filter={filter}")]
ServiceResult GetSchedules(string apiKey, string userAccessCode, string states, string types, string filter);
}
The ServiceResult object contains two elements: State a simple string and Result, which is an object (so it can be anything). If i use object as its type, after deserialization I end up with a list of XmlNodes. So I created an abstract class Result that acts a a superclass for all the expected types:
[Serializable()]
[XmlRoot("ServiceResult", Namespace = "http://schemas.datacontract.org/2004/07/DTOModel.OpenApi")]
public class ServiceResult
{
[XmlElement("Result")]
public Result Result { get; set; }
[XmlElement("State")]
public string State { get; set; }
}
...
[XmlInclude(typeof(UserDto)), XmlInclude(typeof(ScheduleDto))]
[XmlType(Namespace = "http://schemas.datacontract.org/2004/07/DTOModel.Management")]
public abstract class Result
{
}
This works great an I can cast the superclass to the correct subclass after deserialization. But here's my problem: the result can also be an array and I cannot create a class (using CollectionDataContract) that inherits from Result and is an array (or list for that matter)...
Is there a solution or am I looking at this all wrong?
Thanks
精彩评论