EnumMemberAttribute 類別

定義

指定欄位是列舉成員,並且應該加以序列化。

public ref class EnumMemberAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Field, AllowMultiple=false, Inherited=false)]
public sealed class EnumMemberAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field, AllowMultiple=false, Inherited=false)>]
type EnumMemberAttribute = class
    inherit Attribute
Public NotInheritable Class EnumMemberAttribute
Inherits Attribute
繼承
EnumMemberAttribute
屬性

範例

下列範例會示範已套用至列舉成員的 EnumMemberAttribute

[DataContract]
public enum Position
{
    [EnumMember(Value = "Emp")]
    Employee,
    [EnumMember(Value = "Mgr")]
    Manager,
    [EnumMember(Value = "Ctr")]
    Contractor,
    NotASerializableEnumeration
}

[DataContract]
public class Person : IExtensibleDataObject
{
    public Person(string firstNameValue, string lastNameValue)
    {
        LastName = firstNameValue;
        FirstName = lastNameValue;
    }

    private ExtensionDataObject extensioDataValue;
    public ExtensionDataObject ExtensionData
    {
        get { return extensioDataValue; }
        set { extensioDataValue = value; }
    }

    [DataMember]
    internal string FirstName;
    [DataMember]
    internal string LastName;
    [DataMember]
    internal Position Description;
}

public sealed class Test
{
    private Test() { }
    static void Main()
    {

        try
        {

            Test t = new Test();
            t.Serialize("Enum.xml");
            Console.WriteLine("Done");
            Console.ReadLine();
        }
        catch (SerializationException  exc)
        {
            Console.WriteLine(exc.Message);
            Console.ReadLine();
        }
    }

    private void Serialize(string path)
    {
        Console.WriteLine("Serializing...");
        Person p = new Person("Denise", "Smith");
        p.Description = Position.Manager;

        FileStream fs = new FileStream(path, FileMode.Create);

        try
        {
            DataContractSerializer ser =
                new DataContractSerializer(typeof(Person));
            ser.WriteObject(fs, p);
            Console.WriteLine("Done");
        }
        catch (SerializationException exc)
        {
            Console.WriteLine(exc.Message);
            Console.WriteLine(exc.StackTrace);
        }
        finally
        {
            fs.Close();
        }
    }
}
<DataContract()>  _
Public Enum Position
    <EnumMember(Value:="Emp")> Employee
    <EnumMember(Value:="Mgr")> Manager
    <EnumMember(Value:="Ctr")> Contractor
    NotASerializableEnumeration
    
End Enum 

<DataContract()>  _
Public Class Person
    Implements IExtensibleDataObject
    
    Public Sub New(ByVal firstNameValue As String, _
       ByVal lastNameValue As String) 
        LastName = firstNameValue
        FirstName = lastNameValue
    
    End Sub 
    
    Private extensioDataValue As ExtensionDataObject 
    
    Public Property ExtensionData() As ExtensionDataObject _
       Implements IExtensibleDataObject.ExtensionData
        Get
            Return extensioDataValue
        End Get
        Set
            extensioDataValue = value
        End Set
    End Property 

    <DataMember()>  _
    Friend FirstName As String

    <DataMember()>  _
    Friend LastName As String

    <DataMember()>  _
    Friend Description As Position

End Class 


NotInheritable Public Class Test
    
    Private Sub New() 
    
    End Sub
    
    Shared Sub Main() 
        WriteObject("Enum.xml")
        Console.ReadLine()
    
    End Sub 
    
    
    Shared Sub WriteObject(ByVal path As String) 
        Console.WriteLine("Writing...")
        Dim p As New Person("Denise", "Smith")
        p.Description = Position.Manager
        
        Dim fs As New FileStream(path, FileMode.Create)
        
        Try
            Dim ser As New DataContractSerializer(GetType(Person))
            ser.WriteObject(fs, p)
            Console.WriteLine("Done")
        Catch exc As SerializationException
            Console.WriteLine(exc.Message)
            Console.WriteLine(exc.StackTrace)
        Finally
            fs.Close()
        End Try
    
    End Sub 
End Class

備註

EnumMemberAttribute 能夠有效控制列舉的名稱,因為這些是已序列化的名稱。

為了使用 EnumMemberAttribute,此時會建立列舉,然後將 DataContractAttribute 屬性套用至列舉。 接著,將 EnumMemberAttribute 屬性套用至必須出現在序列化 (Serialization) 資料流中的每個成員。

建構函式

EnumMemberAttribute()

初始化 EnumMemberAttribute 類別的新執行個體。

屬性

IsValueSetExplicitly

取得是否已明確設定 Value

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)
Value

取得或設定與已套用屬性之列舉成員相關聯的值。

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

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

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

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

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

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

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱