XmlArrayItemAttribute.Type 속성

정의

배열에 사용할 수 있는 형식을 가져오거나 설정합니다.

public:
 property Type ^ Type { Type ^ get(); void set(Type ^ value); };
public Type Type { get; set; }
public Type? Type { get; set; }
member this.Type : Type with get, set
Public Property Type As Type

속성 값

Type

배열에 사용할 수 있는 Type입니다.

예제

다음 예제에서는 개체 배열을 직렬화합니다. 배열을 반환하는 필드는 두 개의 XmlArrayItemAttribute 인스턴스로 특성이 지정됩니다. 각 인스턴스는 배열에 XmlSerializer 지정된 Type 것을 수락하도록 지시합니다.

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;

public ref class Person
{
public:
   String^ Name;
};

public ref class Manager: public Person
{
public:
   int Rank;
};

public ref class Group
{
public:

   /* The Type property instructs the XmlSerializer to accept both
      the Person and Manager types in the array. */

   [XmlArrayItem(Type=Manager::typeid),
   XmlArrayItem(Type=Person::typeid)]
   array<Person^>^Staff;
};

void SerializeOrder( String^ filename )
{
   // Creates an XmlSerializer.
   XmlSerializer^ xSer = gcnew XmlSerializer( Group::typeid );

   // Creates the Group object, and two array items.
   Group^ myGroup = gcnew Group;
   Person^ p1 = gcnew Person;
   p1->Name = "Jacki";
   Manager^ p2 = gcnew Manager;
   p2->Name = "Megan";
   p2->Rank = 2;
   array<Person^>^myStaff = {p1,p2};
   myGroup->Staff = myStaff;
   
   // Serializes the object, and closes the StreamWriter.
   TextWriter^ writer = gcnew StreamWriter( filename );
   xSer->Serialize( writer, myGroup );
}

int main()
{
   SerializeOrder( "TypeEx.xml" );
}
using System;
using System.IO;
using System.Xml.Serialization;

public class Group
{
   /* The Type property instructs the XmlSerializer to accept both
   the Person and Manager types in the array. */
   [XmlArrayItem(Type = typeof(Manager)),
   XmlArrayItem(Type=typeof(Person))]
   public Person[]Staff;
}

public class Person
{
   public string Name;
}

public class Manager:Person
{
   public int Rank;
}

public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeOrder("TypeEx.xml");
   }

   public void SerializeOrder(string filename)
   {
      // Creates an XmlSerializer.
      XmlSerializer xSer =
      new XmlSerializer(typeof(Group));

      // Creates the Group object, and two array items.
      Group myGroup = new Group();

      Person p1 = new Person();
      p1.Name = "Jacki";
      Manager p2 = new Manager();

      p2.Name = "Megan";
      p2.Rank = 2;

      Person [] myStaff = {p1,p2};
      myGroup.Staff = myStaff;

      // Serializes the object, and closes the StreamWriter.
      TextWriter writer = new StreamWriter(filename);
      xSer.Serialize(writer, myGroup);
   }
}
Imports System.IO
Imports System.Xml.Serialization



Public Class Group
    ' The Type property instructs the XmlSerializer to accept both
    ' the Person and Manager types in the array. 
    <XmlArrayItem(Type := GetType(Manager)), _
     XmlArrayItem(Type := GetType(Person))> _
    Public Staff() As Person
        
End Class


Public Class Person
    Public Name As String
End Class 


Public Class Manager
    Inherits Person
    Public Rank As Integer
End Class 


Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeOrder("TypeEx.xml")
    End Sub 
        
    
    Public Sub SerializeOrder(filename As String)
        ' Creates an XmlSerializer.
        Dim xSer As New XmlSerializer(GetType(Group))
        
        ' Creates the Group object, and two array items.
        Dim myGroup As New Group()
        
        Dim p1 As New Person()
        p1.Name = "Jacki"
        Dim p2 As New Manager()
        
        p2.Name = "Megan"
        p2.Rank = 2
        
        Dim myStaff() As Person =  {p1, p2}
        myGroup.Staff = myStaff
        
        ' Serializes the object, and closes the StreamWriter.
        Dim writer As New StreamWriter(filename)
        xSer.Serialize(writer, myGroup)
    End Sub 
End Class

설명

Type 공용 필드 또는 공용 읽기/쓰기 속성 값에 대해 재정의된 형식을 지정하려면 이 속성을 사용합니다.

필드 또는 속성이 형식 Object배열을 반환하는 경우 필드 또는 속성에 XmlArrayItemAttribute 여러 인스턴스를 적용합니다. 각 인스턴스에 Type 대해 속성을 배열에 삽입할 수 있는 개체 형식으로 설정합니다.

배열에 기본 형식 XmlArrayItemAttribute만 포함된 경우 . 기본적으로 각 값에 XmlSerializer 대해 각각 동일한 요소 이름을 가진 일련의 요소를 생성하지만 각 요소의 형식은 XML 스키마 데이터 형식으로 설정됩니다. 예를 들어, 다음 코드는

' Visual Basic code  
Public Class Arrays  
   Public XSDTypes ()As Object= New Object(){"one", 2, 3.0}  
End Class  
// C# code  
public class MyArray{  
   // No XmlArrayItemAttribute is applied.  
   public object[] XSDTypes= new object[]{"one", 2, 3.2};  
}  

이 XML이 생성됩니다.

<?xml version="1.0" encoding="utf-8"?>  
<Arrays xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  
  <XSDTypes>  
    <Object xsi:type="xsd:string">one</Object>  
    <Object xsi:type="xsd:int">2</Object>  
    <Object xsi:type="xsd:double">3</Object>  
  </XSDTypes>  
</Arrays>  

그러나 각 기본 형식에 Type 대 한 속성을 지정 하는 경우 각 값에 대 한 요소 이름은 .NET 형식 이름을 사용 하 여 생성 됩니다. 예를 들어 이 코드는 다음과 같습니다.

' Visual Basic code  
Public Class Arrays  
   <XmlArrayItem(GetType(String)), _  
   XmlArrayItem(GetType(Integer)), _  
   XmlArrayItem(GetType(Double))> _  
   Public PrimitiveTypes () As Object = New Object(){"one", 2, 3.0}  
End Class  
// C# code  
public class Arrays{  
   [XmlArrayItem(typeof(string))]  
   [XmlArrayItem(typeof(int))]  
   [XmlArrayItem(typeof(double))]  
   public object [] PrimitiveTypes = new object[]{"one", 2, 3.0};  
}  

이 XML이 생성됩니다.

<?xml version="1.0" encoding="utf-8"?>  
<Arrays xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  
  <PrimitiveTypes>  
    <string>one</string>  
    <int>2</int>  
    <double>3</double>  
  </PrimitiveTypes>  
</Arrays>  

적용 대상