IDataContractSurrogate.GetDataContractType(Type) Method

Definition

During serialization, deserialization, and schema import and export, returns a data contract type that substitutes the specified type.

public Type GetDataContractType (Type type);

Parameters

type
Type

The CLR type Type to substitute.

Returns

The Type to substitute for the type value. This type must be serializable by the DataContractSerializer. For example, it must be marked with the DataContractAttribute attribute or other mechanisms that the serializer recognizes.

Examples

The following example shows an implementation of the GetDataContractType method.

    public Type GetDataContractType(Type type)
{
        Console.WriteLine("GetDataContractType invoked");
        Console.WriteLine("\t type name: {0}", type.Name);
        // "Person" will be serialized as "PersonSurrogated"
        // This method is called during serialization,
        // deserialization, and schema export.
        if (typeof(Person).IsAssignableFrom(type))
{
Console.WriteLine("\t returning PersonSurrogated");
            return typeof(PersonSurrogated);
        }
        return type;
    }

Applies to

Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1