XmlIncludeAttribute 類別


XmlSerializer 在對物件進行序列化或還原序列化時,能夠辨識型別。Allows the XmlSerializer to recognize a type when it serializes or deserializes an object.

public ref class XmlIncludeAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Struct, AllowMultiple=true)]
public class XmlIncludeAttribute : Attribute
public class XmlIncludeAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method | System.AttributeTargets.Struct, AllowMultiple=true)]
public class XmlIncludeAttribute : Attribute
type XmlIncludeAttribute = class
    inherit Attribute
Public Class XmlIncludeAttribute
Inherits Attribute


下列範例顯示三個類別,其中兩個會繼承自第三個。The following example shows three classes, two of which inherit from the third. 此範例會將 XmlIncludeAttribute 套用至方法,以傳回其中一個衍生類別的實例。The example applies the XmlIncludeAttribute to a method that returns an instance of one of the derived classes.

<%@ WebService Language="C#" Class="Test" %>
using System;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Services.Description;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Data;
public class Test : WebService {
   [return:XmlElement("MyTime", DataType="time")]
   public DateTime EchoString([XmlElement(DataType="string")] 
   string strval) {
        return DateTime.Now;
   [XmlInclude(typeof(Car)), XmlInclude(typeof(Bike))]
   public Vehicle Vehicle(string licenseNumber) {
      if (licenseNumber == "0") {
         Vehicle v = new Car();
         v.licenseNumber = licenseNumber;
         return v;
      else if (licenseNumber == "1") {
          Vehicle v = new Bike();
          v.licenseNumber = licenseNumber;
          return v;
      else {
         return null;
public abstract class Vehicle {
    public string licenseNumber;
    public DateTime make;
public class Car : Vehicle {
public class Bike : Vehicle {


當您呼叫 XmlSerializer 類別的 SerializeDeserialize 方法時,請使用 XmlIncludeAttributeUse the XmlIncludeAttribute when you call the Serialize or Deserialize method of the XmlSerializer class.

套用 XmlIncludeAttribute時,請指定衍生類別的 TypeWhen applying the XmlIncludeAttribute, specify the Type of the derived class. XmlSerializer 序列化同時包含基底和衍生類別的物件時,它就可以辨識這兩個物件類型。When the XmlSerializer serializes objects that include both the base and the derived class, it can then recognize both object types.

您可以使用 XmlIncludeAttribute,在服務描述檔中包含以 Web 服務描述語言(WSDL)撰寫的衍生類別。You can use the XmlIncludeAttribute to include derived classes in service description documents that are written in the Web Services Description Language (WSDL). 例如,如果方法傳回 Object,請將 XmlIncludeAttribute 套用至方法,並指定要傳回的實際類型。For example, if a method returns an Object, apply the XmlIncludeAttribute to the method and specify the actual types to return.

如需 WSDL 的詳細資訊,請參閱Web 服務描述語言(WSDL) 1.1For more information on the WSDL, see Web Services Description Language (WSDL) 1.1.



XmlIncludeAttribute 類別的新執行個體初始化。Initializes a new instance of the XmlIncludeAttribute class.



取得或設定要包含的物件的型別。Gets or sets the type of the object to include.


在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。When implemented in a derived class, gets a unique identifier for this Attribute.

(繼承來源 Attribute)



傳回值,這個值指出此執行個體是否與指定的物件相等。Returns a value that indicates whether this instance is equal to a specified object.

(繼承來源 Attribute)

傳回這個執行個體的雜湊碼。Returns the hash code for this instance.

(繼承來源 Attribute)

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(繼承來源 Attribute)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(繼承來源 Attribute)

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)


_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。Maps a set of names to a corresponding set of dispatch identifiers.

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。Retrieves the type information for an object, which can be used to get the type information for an interface.

(繼承來源 Attribute)

擷取物件提供的類型資訊介面數目 (0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。Provides access to properties and methods exposed by an object.

(繼承來源 Attribute)