namespace Madgeek
{
#region Imports
using System;
using System.Collections;
using System.Reflection;
#endregion
///
/// Used to associate string values to items.
///
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class StringValueAttribute : Attribute
{
private string m_Value;
public StringValueAttribute(string value)
{
m_Value = value;
}
public string Value
{
get { return m_Value; }
set { m_Value = value; }
}
///
/// Returns the of the first of an enum.
///
/// The object which string value is asked.
/// null if no is found.
static public string GetValue(Enum e)
{
StringValueAttribute attribute;
FieldInfo field;
field = e.GetType().GetField(e.ToString());
attribute = GetCustomAttribute(field, MethodBase.GetCurrentMethod().DeclaringType) as StringValueAttribute;
if (attribute == null)
return null;
return attribute.Value;
}
///
/// Returns an array containing the of each of the attributes of an enum.
///
/// The object which string values are asked.
/// null if no is found.
static public string[] GetValues(Enum e)
{
Attribute[] attributes;
FieldInfo field;
ArrayList result;
field = e.GetType().GetField(e.ToString());
attributes = GetCustomAttributes(field, MethodBase.GetCurrentMethod().DeclaringType);
if (attributes == null)
return null;
result = new ArrayList(attributes.Length);
foreach (StringValueAttribute attribute in attributes)
result.Add(attribute.Value);
return (string[]) result.ToArray(typeof(string));
}
}
}