Microsoft Docs

EnumMemberValues

Overview

The EnumMemberValues class provides a collection of values that can be associated with an enumeration member for serialization purposes. It is used internally by the EnumMemberAttribute to specify custom values for enum members when using the DataContractSerializer.

Syntax

public sealed class EnumMemberValues : System.Collections.ObjectModel.Collection<EnumMemberValue>

Namespace: System.Runtime.Serialization

Assembly: System.Runtime.Serialization.dll

Properties

Methods

Example

The following example demonstrates how to use EnumMemberValues with a custom enumeration.

using System;
using System.Runtime.Serialization;

[DataContract]
public enum Status
{
    [EnumMember(Value = "0")]
    Unknown = 0,

    [EnumMember(Value = "1")]
    Active = 1,

    [EnumMember(Value = "2")]
    Inactive = 2
}

class Program
{
    static void Main()
    {
        var serializer = new DataContractSerializer(typeof(Status));
        var active = Status.Active;
        using var stream = new System.IO.MemoryStream();
        serializer.WriteObject(stream, active);
        stream.Position = 0;
        var xml = new System.IO.StreamReader(stream).ReadToEnd();
        Console.WriteLine(xml);
    }
}

See Also