XmlIncludeAttribute(Type) 构造函数

定义

初始化 XmlIncludeAttribute 类的新实例。

public:
 XmlIncludeAttribute(Type ^ type);
public XmlIncludeAttribute (Type type);
public XmlIncludeAttribute (Type? type);
new System.Xml.Serialization.XmlIncludeAttribute : Type -> System.Xml.Serialization.XmlIncludeAttribute
Public Sub New (type As Type)

参数

type
Type

要包含的对象的 Type

示例

以下示例演示三个类,其中两个类继承自第三个类。 该示例将应用于 XmlIncludeAttribute 返回两个派生类之一的实例的方法。 该示例将 Type 属性设置为返回的对象的类型。

public ref class Vehicle{};

public ref class Car: public Vehicle{};

public ref class Truck: public Vehicle{};

public ref class Sample
{
public:

   [WebMethodAttribute]
   [XmlInclude(Car::typeid)]
   [XmlInclude(Truck::typeid)]
   Vehicle^ ReturnVehicle( int i )
   {
      if ( i == 0 )
            return gcnew Car;
      else
            return gcnew Truck;
   }
};
public class Vehicle{}

public class Car:Vehicle{}

public class Truck:Vehicle{}

public class Sample
{
[WebMethodAttribute]
[XmlInclude(typeof(Car))]
[XmlInclude(typeof(Truck))]
public Vehicle ReturnVehicle(int i){
   if(i == 0)
      return new Car();
   else
      return new Truck();
   }
}
Public Class Vehicle
End Class
 
Public Class Car
    Inherits Vehicle
End Class
 
Public Class Truck
    Inherits Vehicle
End Class
 
Public Class Sample    
    <WebMethod(), _
     XmlInclude(GetType(Car)), _
     XmlInclude(GetType(Truck))> _
    Public Function ReturnVehicle(i As Integer) As Vehicle
        
        If i = 0 Then
            Return New Car()
        Else
            Return New Truck()
        End If
    End Function
End Class

适用于