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
Count– Gets the number of elements contained in the collection.Item[int index]– Gets theEnumMemberValueat the specified index.
Methods
Add(EnumMemberValue item)– Adds anEnumMemberValueto the collection.Clear()– Removes all items from the collection.Contains(EnumMemberValue item)– Determines whether the collection contains a specific value.CopyTo(EnumMemberValue[] array, int arrayIndex)– Copies the collection elements to an existing one-dimensional array.Remove(EnumMemberValue item)– Removes the first occurrence of a specific object.
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);
}
}