using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace IRIGCh10
{
///
/// allows annotating enums with a description attribute, in chapter 10 this allows us
/// to associate an enum with a string to put in the TMATS file for that enum
///
public static class DescriptionDecoder
{
public static string GetDescription(Enum value)
{
var fi = value.GetType().GetField(value.ToString());
var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false);
if (attributes.Any())
{
return attributes.First().Description;
}
return value.ToString();
}
}
///
/// a lot of attributes in chapter 10 have a max length associated with them
/// note however though that max length in chapter 10 is just a suggested
/// max length, not a requirement ...
///
public static class MaxLengthDecoder
{
public static int GetMaxLength(Enum value)
{
var fi = value.GetType().GetField(value.ToString());
var attributes = (MaxLengthAttribute[])fi.GetCustomAttributes(
typeof(MaxLengthAttribute), false);
if (attributes.Any())
{
return attributes.First().Length;
}
return 0;
}
}
}