This documentation is archived and is not being maintained.

KnownTypeAttribute Class

Specifies types that should be recognized by the DataContractSerializer when serializing or deserializing a given type.

Namespace:  System.Runtime.Serialization
Assembly:  System.Runtime.Serialization (in System.Runtime.Serialization.dll)

'Declaration
<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Struct, Inherited := True,  _
	AllowMultiple := True)> _
Public NotInheritable Class KnownTypeAttribute _
	Inherits Attribute
'Usage
Dim instance As KnownTypeAttribute

Apply the KnownTypeAttribute attribute to a type to indicate to the DataContractSerializer types that should be recognized when serializing or deserializing an instance of the type to which the attribute is applied. This attribute could also be recognized by other serializers that understand data contracts.

NoteNote:

In your code, you can use the word KnownType instead of the longer KnownTypeAttribute.

You can either apply exactly one KnownTypeAttribute instance with the MethodName property set, or one or more KnownTypeAttribute instances with the Type property set.

The following example shows a type named Person and a type named IDInformation that should be recognized when serializing or deserializing the Person type.

Imports System
Imports System.Collections.Generic
Imports System.Collections
Imports System.Text
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Xml

Class Program

    Shared Sub Main(ByVal args() As String)
        Try
            Serialize("KnownTypeAttributeExample.xml")
            Deserialize("KnownTypeAttributeExample.xml")
            ' Run this twice. The second time, comment out the 
            ' Serialize call and comment out the  
            ' KnownTypeAttribute on the Person class. The 
            ' deserialization will then fail. 
        Catch exc As SerializationException
            Console.WriteLine("{0}: {1}", exc.Message, exc.StackTrace)
        Finally
            Console.WriteLine("Press Enter to exit...")
            Console.ReadLine()
        End Try 

    End Sub 


    Public Shared Sub Serialize(ByVal path As String)
        Dim p As New Person()
        p.Miscellaneous.Add(DateTime.Now, "Hello")
        p.Miscellaneous.Add(DateTime.Now.AddSeconds(1), "World")
        Dim w As New IDInformation()
        w.ID = "1111 00000"
        p.Miscellaneous.Add(DateTime.Now.AddSeconds(2), w)
        Dim ser As New DataContractserializer(GetType(Person))
        Using fs As New FileStream(path, FileMode.OpenOrCreate)
            ser.WriteObject(fs, p)
        End Using 
    End Sub 

    Public Shared Sub Deserialize(ByVal path As String)
        Dim ser As New DataContractserializer(GetType(Person))
        Using fs As New FileStream(path, FileMode.OpenOrCreate)
            Dim p2 As Person = ser.ReadObject(fs)
            Console.WriteLine("Count {0}", p2.Miscellaneous.Count)
            For Each de As DictionaryEntry In p2.Miscellaneous
                Console.WriteLine("Key {0} Value: {1}", de.Key, _
                de.Value)
                If TypeOf (de.Value) Is IDInformation Then 
                    Dim www As IDInformation = de.Value
                    Console.WriteLine( _
                    "Found ID Information. ID: {0}", www.ID)
                End If 
            Next 
        End Using 
    End Sub 

    ' Apply the KnownTypeAttribute to the class that  
    ' includes a member that returns a Hashtable.
    <System.Runtime.Serialization.KnownType(GetType(IDInformation))> _
    <DataContract()> _
    Public Class Person
        Implements IExtensibleDataObject
        Private MiscellaneousValue As New Hashtable()
        Private ExtensionDataObjectValue As ExtensionDataObject

        Public Property ExtensionData() As ExtensionDataObject _
            Implements IExtensibleDataObject.ExtensionData
            Get 
                Return ExtensionDataObjectValue
            End Get 
            Set(ByVal value As ExtensionDataObject)
                ExtensionDataObjectValue = value
            End Set 
        End Property

        <DataMember()> _
        Public Property Miscellaneous() As Hashtable
            Get 
                Return MiscellaneousValue
            End Get 
            Set(ByVal value As Hashtable)
                MiscellaneousValue = value
            End Set 
        End Property 
    End Class

    <DataContract()> _
    Public Class IDInformation
        Implements IExtensibleDataObject

        Private ExtensionDataObjectValue As ExtensionDataObject

        Public Property ExtensionData() As ExtensionDataObject _
            Implements IExtensibleDataObject.ExtensionData
            Get 
                Return ExtensionDataObjectValue
            End Get 

            Set(ByVal value As ExtensionDataObject)
                ExtensionDataObjectValue = value
            End Set 
        End Property

        <DataMember()> _
        Public ID As String 
    End Class 
End Class

System.Object
  System.Attribute
    System.Runtime.Serialization.KnownTypeAttribute

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Show: