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)

適用対象

こちらもご覧ください