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 属性。

构造函数

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)

将一组名称映射为对应的一组调度标识符。

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。

(继承自 Attribute)

适用于

另请参阅