[System.AttributeUsage(System.AttributeTargets.Field)]
public class PrimaryKeySegmentAttribute : System.Attribute
public readonly string Value;
public PrimaryKeySegmentAttribute(string value)
[System.AttributeUsage(System.AttributeTargets.Field)]
public class UrnSegmentAttribute : System.Attribute
public readonly string Value;
public UrnSegmentAttribute(string value)
public enum ExchangeSegment
[PrimaryKeySegment("FL")]
[UrnSegment("contact-ref")]
public static class SegmentExtensionMethods
public static string ToPrimaryKeySegment<T>(this T value)
throw new ArgumentException("T must be an enumerated type");
FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
var attribute = (PrimaryKeySegmentAttribute)fieldInfo.GetCustomAttribute(typeof(PrimaryKeySegmentAttribute));
public static string ToUrnSegment<T>(this T value)
throw new ArgumentException("T must be an enumerated type");
FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
var attribute = (UrnSegmentAttribute)fieldInfo.GetCustomAttribute(typeof(UrnSegmentAttribute));
return value.ToString().ToLower();
public static void Main()
var segment = ExchangeSegment.File;
Console.WriteLine("PK Segment is " + segment.ToPrimaryKeySegment());
Console.WriteLine("UrnSegment is " + segment.ToUrnSegment());
segment = ExchangeSegment.Contact;
Console.WriteLine("Segment is " + segment.ToPrimaryKeySegment());
Console.WriteLine("UrnSegment is " + segment.ToUrnSegment());