Blogger :
Thinktecture Blog
All posts :
All posts by Thinktecture Blog
Category :
WSCF/WCF
Blogged date : 2006 Sep 06
Caution: The following is undocumented and might be changed or removed at any time without notice.
WCF's serialization of data contracts supports the transmission of derived types which have not been available when the original application (or just contract) has been built. In the following example, ApplicationExtensionObject is just a placeholder instead of which a derived class should be transferred at runtime.
[DataContract]
public class Customer
{
[DataMember]
public string Firstname;
[DataMember]
public string Lastname;
[DataMember]
public Address DefaultDeliveryAddress;
[DataMember]
public Address DefaultBillingAddress;
[DataMember]
public ApplicationExtensionObject Extension;
}
[DataContract]
public class ApplicationExtensionObject
{
}
You would normally accomplish this using the [KnownType] attribute on the field Extension. But what if you can't do this as the type is not yet known at compile time? Well, for one thing you could use [KnownType(methodName)] to define a method which returns an array of Types at runtime. Another alternative is to use the section to define the mapping between declared types and known subtypes.
In the following snippet, the class ProjectSpecificExtension.CustomerExtension is defined as a valid known subtype of ApplicationExtensionObject for the serializer.
<configuration >
<system.runtime.serialization>
<dataContractSerializer>
<declaredTypes>
<add type="Shared.ApplicationExtensionObject, Shared">
<knownType type="ProjectSpecificExtension.CustomerExtension, ProjectSpecificExtension" />
</add>
</declaredTypes>
</dataContractSerializer>
</system.runtime.serialization>
...
</configuration>
Repeat warning: This is not documented and I got it from decompiling System.Runtime.Serialization.Configuration. It might therefore change (or even be removed) with or without notice. This post is therefore basically a note to self to check it at RTM ;-).