共用方式為


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 屬性設定)。

如需數據合約和已知型別的詳細資訊,請參閱 數據合約已知型別

適用於

另請參閱