WCF DataContract Upcasting

Posted by Jarred Froman on Stack Overflow See other posts from Stack Overflow or by Jarred Froman
Published on 2009-06-21T20:52:03Z Indexed on 2010/05/07 1:08 UTC
Read the original article Hit count: 293

Filed under:
|
|

I'm trying to take a datacontract object that I received on the server, do some manipulation on it and then return an upcasted version of it however it doesn't seem to be working. I can get it to work by using the KnownType or ServiceKnownType attributes, but I don't want to roundtrip all of the data. Below is an example:

[DataContract]
public class MyBaseObject
{
    [DataMember]
    public int Id { get; set; }
}

[DataContract]
public class MyDerivedObject : MyBaseObject
{
    [DataMember]
    public string Name { get; set; }
}


[ServiceContract(Namespace = "http://My.Web.Service")]
public interface IServiceProvider
{
    [OperationContract]
    List<MyBaseObject> SaveMyObjects(List<MyDerivedObject> myDerivedObjects);
}

public class ServiceProvider : IServiceProvider
{
    public List<MyBaseObject> SaveMyObjects(List<MyDerivedObject> myDerivedObjects)
    {
        ... do some work ...

        myDerivedObjects[0].Id = 123;
        myDerivedObjects[1].Id = 456;
        myDerivedObjects[2].Id = 789;

        ... do some work ...

        return myDerivedObjects.Cast<MyBaseObject>().ToList();
    }
}

Anybody have any ideas how to get this to work without having to recreate new objects or using the KnownType attributes?

© Stack Overflow or respective owner

Related posts about wcf

Related posts about datacontract