« August 2006 | Main | November 2006 »
Posted at 09:37 AM | Permalink | Comments (2) | TrackBack (0)
Posted at 03:07 PM | Permalink | Comments (1) | TrackBack (0)
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>Update: This has actually been documented and is a supported practice. I guess I just made it too much of a habit to look into Reflector instead of searching long enough in the docs ;-)
Posted at 08:03 PM | Permalink | Comments (3) | TrackBack (0)
Posted at 03:47 PM | Permalink | Comments (3) | TrackBack (0)
Posted at 10:20 PM | Permalink | Comments (1) | TrackBack (0)