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 ?

Nicolas
  • 11
  • 1
  • 2

2 Answers2

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • My problem is over Field class, not BusinessObject which is erializing well. – Nicolas Sep 12 '11 at 15:46
  • @Nicolas, oh sorry. In this case you should decorate your `Field` class with the `KnownType` attribute by specifying all possible child objects. – Darin Dimitrov Sep 12 '11 at 15:46
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.

hungryMind
  • 6,931
  • 4
  • 29
  • 45