1

I'm trying to make a class pass through WCF. These are no problems except for my abstract class which isn't serialized. Is there any way to avoid that ?

[DataContract]
[KnownType("GetKnownTypes")]
public class BusinessObject
{
    public static Type[] GetKnownTypes()
    {
        // only returns the different types my "Field" abstract class can take
        return Services.WCFRIAKnownTypesHelperService.FieldsKnownTypes.ToArray();
    }

    [DataMember]
    public String ID { get; set; }
    [DataMember]
    public List<Section> Sections { get; set; }
    [DataMember]
    public List<Field> Fields { get; set; }
}

And now my field class

[DataContract]
public abstract class Field 
{
    [DataMember]
    public String FieldID { get; set; }
    [DataMember]
    public String Title { get; set; }
    [DataMember]
    public Object Content { get;set; }
}

Why isn't it working ?

2 Answers 2

3

The [KnownType] attribute expects a static type to be passed which must be known at compile time:

[DataContract]
[KnownType(typeof(SomeChildOfBusinessObject))]
[KnownType(typeof(SomeOtherChildOfBusinessObject))]
public class BusinessObject
{
    ...
}

If you want to register dynamic known types you may take a look at the following blog post.

Sign up to request clarification or add additional context in comments.

2 Comments

My problem is over Field class, not BusinessObject which is erializing well.
@Nicolas, oh sorry. In this case you should decorate your Field class with the KnownType attribute by specifying all possible child objects.
0

You cannot transport unknown types to client though if inherited by knowntype. When casting ur types to base class, the object still remain to child class and hence cannot be transported or serialized. You can use refecltion to register dynamic types as known types but again this might be problem deserializing dynamic types.

2 Comments

Ok, I see. Is there any way, as i'm using RIA services, to use "shared context" to do it ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.