ExportOptions.KnownTypes 属性

定义

获取在序列化或反序列化过程中可能会遇到的类型的集合。

public:
 property System::Collections::ObjectModel::Collection<Type ^> ^ KnownTypes { System::Collections::ObjectModel::Collection<Type ^> ^ get(); };
public System.Collections.ObjectModel.Collection<Type> KnownTypes { get; }
member this.KnownTypes : System.Collections.ObjectModel.Collection<Type>
Public ReadOnly Property KnownTypes As Collection(Of Type)

属性值

一个 KnownTypes 集合,它包含在序列化或反序列化过程中可能会遇到的类型。 将为此集合中由 XsdDataContractExporter 指定的所有类型导出 XML 架构表示形式。

示例

下面的示例创建 ExportOptions 类的实例,并将某种类型添加到 KnownTypes 属性返回的集合中。

static void ExportXSD()
{
    XsdDataContractExporter exporter = new XsdDataContractExporter();
    if (exporter.CanExport(typeof(Employee)))
    {
        exporter.Export(typeof(Employee));
        Console.WriteLine("number of schemas: {0}", exporter.Schemas.Count);
        Console.WriteLine();
        XmlSchemaSet mySchemas = exporter.Schemas;

        XmlQualifiedName XmlNameValue = exporter.GetRootElementName(typeof(Employee));
        string EmployeeNameSpace = XmlNameValue.Namespace;

        foreach (XmlSchema schema in mySchemas.Schemas(EmployeeNameSpace))
        {
            schema.Write(Console.Out);
        }
    }
}
Shared Sub ExportXSD() 

    Dim exporter As New XsdDataContractExporter()

    ' Use the ExportOptions to add the Possessions type to the 
    ' collection of KnownTypes. 
    Dim eOptions As New ExportOptions()
    eOptions.KnownTypes.Add(GetType(Possessions))        
    exporter.Options = eOptions

    If exporter.CanExport(GetType(Employee)) Then
        exporter.Export(GetType(Employee))
        Console.WriteLine("number of schemas: {0}", exporter.Schemas.Count)
        Console.WriteLine()
        Dim mySchemas As XmlSchemaSet = exporter.Schemas
        
        Dim XmlNameValue As XmlQualifiedName = _
           exporter.GetRootElementName(GetType(Employee))
        Dim EmployeeNameSpace As String = XmlNameValue.Namespace
        
        Dim schema As XmlSchema
        For Each schema In  mySchemas.Schemas(EmployeeNameSpace)
            schema.Write(Console.Out)
        Next schema
    End If

End Sub

注解

KnownTypes 使用 DataContractSerializer 属性来包含可能在对象图中读取的类型(是使用 DataContractSerializer.KnownTypes 属性设置的)。

有关数据协定和已知类型的详细信息,请参阅 数据协定已知类型

适用于

另请参阅