SoapEnumAttribute Kelas
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mengontrol bagaimana XmlSerializer menserialisasikan anggota enumerasi.
public ref class SoapEnumAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field)]
public class SoapEnumAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field)>]
type SoapEnumAttribute = class
inherit Attribute
Public Class SoapEnumAttribute
Inherits Attribute
- Warisan
- Atribut
Contoh
Contoh berikut menggunakan XmlSerializer untuk membuat serial kelas bernama Food yang menyertakan enumerasi bernama FoodType. Enumerasi FoodType ditimpa dengan membuat SoapEnumAttribute untuk setiap enumerasi dan mengatur SoapEnum properti SoapAttributes ke SoapEnumAttribute.
SoapAttributes ditambahkan ke yang SoapAttributeOverrides digunakan untuk membuat XmlSerializer.
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class Group{
public string GroupName;
public GroupType Grouptype;
}
public enum GroupType{
// Use the SoapEnumAttribute to instruct the XmlSerializer
// to generate Small and Large instead of A and B.
[SoapEnum("Small")]
A,
[SoapEnum("Large")]
B
}
public class Run {
static void Main(){
Run test= new Run();
test.SerializeObject("SoapEnum.xml");
test.SerializeOverride("SoapOverride.xml");
Console.WriteLine("Fininished writing two files");
}
private void SerializeObject(string filename){
// Create an instance of the XmlSerializer Class.
XmlTypeMapping mapp =
(new SoapReflectionImporter()).ImportTypeMapping(typeof(Group));
XmlSerializer mySerializer = new XmlSerializer(mapp);
// Writing the file requires a TextWriter.
TextWriter writer = new StreamWriter(filename);
// Create an instance of the Class that will be serialized.
Group myGroup = new Group();
// Set the object properties.
myGroup.GroupName = ".NET";
myGroup.Grouptype= GroupType.A;
// Serialize the Class, and close the TextWriter.
mySerializer.Serialize(writer, myGroup);
writer.Close();
}
private void SerializeOverride(string fileName){
SoapAttributeOverrides soapOver = new SoapAttributeOverrides();
SoapAttributes SoapAtts = new SoapAttributes();
// Add a SoapEnumAttribute for the GroupType.A enumerator.
// Instead of 'A' it will be "West".
SoapEnumAttribute soapEnum = new SoapEnumAttribute("West");
// Override the "A" enumerator.
SoapAtts.SoapEnum = soapEnum;
soapOver.Add(typeof(GroupType), "A", SoapAtts);
// Add another SoapEnumAttribute for the GroupType.B enumerator.
// Instead of //B// it will be "East".
SoapAtts= new SoapAttributes();
soapEnum = new SoapEnumAttribute();
soapEnum.Name = "East";
SoapAtts.SoapEnum = soapEnum;
soapOver.Add(typeof(GroupType), "B", SoapAtts);
// Create an XmlSerializer used for overriding.
XmlTypeMapping map =
new SoapReflectionImporter(soapOver).
ImportTypeMapping(typeof(Group));
XmlSerializer ser = new XmlSerializer(map);
Group myGroup = new Group();
myGroup.GroupName = ".NET";
myGroup.Grouptype = GroupType.B;
// Writing the file requires a TextWriter.
TextWriter writer = new StreamWriter(fileName);
ser.Serialize(writer, myGroup);
writer.Close();
}
}
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Public Class Group
Public GroupName As String
Public Grouptype As GroupType
End Class
Public enum GroupType
' Use the SoapEnumAttribute to instruct the XmlSerializer
' to generate Small and Large instead of A and B.
<SoapEnum("Small")> _
A
<SoapEnum("Large")> _
B
End enum
Public Class Run
Public Shared Sub Main()
Dim test As Run = new Run()
test.SerializeObject("SoapEnum.xml")
test.SerializeOverride("SoapOverride.xml")
Console.WriteLine("Fininished writing two files")
End Sub
Private Shared Sub SerializeObject(filename As string)
' Create an instance of the XmlSerializer Class.
Dim mapp As XmlTypeMapping = _
(New SoapReflectionImporter()).ImportTypeMapping(GetType(Group))
Dim mySerializer As XmlSerializer = New XmlSerializer(mapp)
' Writing the file requires a TextWriter.
Dim writer As TextWriter = New StreamWriter(filename)
' Create an instance of the Class that will be serialized.
Dim myGroup As Group = New Group()
' Set the object properties.
myGroup.GroupName = ".NET"
myGroup.Grouptype= GroupType.A
' Serialize the Class, and close the TextWriter.
mySerializer.Serialize(writer, myGroup)
writer.Close()
End Sub
Private Sub SerializeOverride(fileName As String)
Dim soapOver As SoapAttributeOverrides = new SoapAttributeOverrides()
Dim SoapAtts As SoapAttributes = new SoapAttributes()
' Add a SoapEnumAttribute for the GroupType.A enumerator. Instead
' of 'A' it will be "West".
Dim soapEnum As SoapEnumAttribute = new SoapEnumAttribute("West")
' Override the "A" enumerator.
SoapAtts.SoapEnum = soapEnum
soapOver.Add(GetType(GroupType), "A", SoapAtts)
' Add another SoapEnumAttribute for the GroupType.B enumerator.
' Instead of 'B' it will be "East".
SoapAtts= New SoapAttributes()
soapEnum = new SoapEnumAttribute()
soapEnum.Name = "East"
SoapAtts.SoapEnum = soapEnum
soapOver.Add(GetType(GroupType), "B", SoapAtts)
' Create an XmlSerializer used for overriding.
Dim map As XmlTypeMapping = New SoapReflectionImporter _
(soapOver).ImportTypeMapping(GetType(Group))
Dim ser As XmlSerializer = New XmlSerializer(map)
Dim myGroup As Group = New Group()
myGroup.GroupName = ".NET"
myGroup.Grouptype = GroupType.B
' Writing the file requires a TextWriter.
Dim writer As TextWriter = New StreamWriter(fileName)
ser.Serialize(writer, myGroup)
writer.Close
End Sub
End Class
Keterangan
Kelas SoapEnumAttribute milik keluarga atribut yang mengontrol bagaimana XmlSerializer serialisasi atau deserialisasi objek sebagai XML SOAP yang dikodekan. XML yang dihasilkan sesuai dengan bagian 5 dari dokumen World Wide Web Consortium Simple Object Access Protocol (SOAP) 1.1. Untuk daftar lengkap atribut serupa, lihat Atribut yang Mengontrol Serialisasi SOAP yang Dikodekan.
Untuk membuat serial objek sebagai pesan SOAP yang dikodekan, Anda harus membuat XmlSerializer menggunakan yang XmlTypeMapping dibuat dengan ImportTypeMapping metode SoapReflectionImporter kelas .
SoapEnumAttribute Gunakan untuk mengubah enumerasi yang XmlSerializer dihasilkan atau dikenali (saat menserialisasikan atau mendeserialisasi kelas). Misalnya, jika enumerasi berisi anggota bernama One, tetapi Anda lebih suka output XML diberi nama Single, terapkan SoapEnumAttribute ke anggota enumerasi dan atur Name properti ke "Tunggal".
Anda dapat mengambil Name alih nilai properti dengan SoapEnumAttribute membuat instans SoapEnumAttribute kelas dan menetapkannya ke SoapEnum properti .SoapAttributes Untuk detailnya, lihat SoapAttributeOverrides gambaran umum kelas.
Untuk membuat serial objek sebagai pesan SOAP yang dikodekan, Anda harus membuat XmlSerializer menggunakan yang XmlTypeMapping dibuat dengan ImportTypeMapping metode SoapReflectionImporter kelas .
Nota
Anda dapat menggunakan kata SoapEnum dalam kode Anda alih-alih lebih lama SoapEnumAttribute.
Untuk informasi selengkapnya tentang menggunakan atribut, lihat Atribut.
Konstruktor
| Nama | Deskripsi |
|---|---|
| SoapEnumAttribute() |
Menginisialisasi instans baru dari kelas SoapEnumAttribute. |
| SoapEnumAttribute(String) |
Menginisialisasi instans SoapEnumAttribute baru kelas menggunakan nama elemen yang ditentukan. |
Properti
| Nama | Deskripsi |
|---|---|
| Name |
Mendapatkan atau mengatur nilai yang dihasilkan dalam dokumen XML saat XmlSerializer menserialisasikan enumerasi, atau nilai yang dikenali saat mendeserialisasi anggota enumerasi. |
| TypeId |
Ketika diimplementasikan dalam kelas turunan, mendapatkan pengidentifikasi unik untuk Attributeini. (Diperoleh dari Attribute) |
Metode
| Nama | Deskripsi |
|---|---|
| Equals(Object) |
Mengembalikan nilai yang menunjukkan apakah instans ini sama dengan objek tertentu. (Diperoleh dari Attribute) |
| GetHashCode() |
Mengembalikan kode hash untuk instans ini. (Diperoleh dari Attribute) |
| GetType() |
Mendapatkan Type instans saat ini. (Diperoleh dari Object) |
| IsDefaultAttribute() |
Ketika ditimpa dalam kelas turunan, menunjukkan apakah nilai instans ini adalah nilai default untuk kelas turunan. (Diperoleh dari Attribute) |
| Match(Object) |
Saat ditimpa dalam kelas turunan, mengembalikan nilai yang menunjukkan apakah instans ini sama dengan objek tertentu. (Diperoleh dari Attribute) |
| MemberwiseClone() |
Membuat salinan dangkal dari Objectsaat ini. (Diperoleh dari Object) |
| ToString() |
Mengembalikan string yang mewakili objek saat ini. (Diperoleh dari Object) |
Implementasi Antarmuka Eksplisit
| Nama | Deskripsi |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
Memetakan sekumpulan nama ke sekumpulan pengidentifikasi pengiriman yang sesuai. (Diperoleh dari Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
Mengambil informasi jenis untuk objek, yang dapat digunakan untuk mendapatkan informasi jenis untuk antarmuka. (Diperoleh dari Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
Mengambil jumlah antarmuka informasi jenis yang disediakan objek (baik 0 atau 1). (Diperoleh dari Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
Menyediakan akses ke properti dan metode yang diekspos oleh objek. (Diperoleh dari Attribute) |