Skip to content

Instantly share code, notes, and snippets.

@EifelMono
Created March 22, 2014 18:20
Show Gist options
  • Save EifelMono/9711840 to your computer and use it in GitHub Desktop.
Save EifelMono/9711840 to your computer and use it in GitHub Desktop.
EnumConverter for differerent enum <-> text handling
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
namespace EifelMono.Core
{
// [Obsolete ("Property is deprecated please use Property instead.")]
#region class Enums
public enum AutoDetection
{
No,
Yes,
YesButLower,
YesButUpper
}
public enum TextContent
{
Default,
Lower,
Upper
}
#endregion
public class EnumConverter<T> where T : struct
{
#region Constructors
protected EnumConverter(string prefix, TextContent textContent, AutoDetection autoDetection)
{
if (!typeof(T).GetTypeInfo().IsEnum)
throw new Exception("T {0} is not an enum", typeof(T).Name);
m_Prefix = prefix;
m_TextContent = textContent;
m_AutoDetection = autoDetection;
}
public EnumConverter(string prefix = "") : this(prefix, TextContent.Default, AutoDetection.No)
{
}
public EnumConverter(AutoDetection autoDetection, string prefix= "", TextContent textContent= TextContent.Default): this(prefix, textContent, autoDetection)
{
}
public EnumConverter(): this("", TextContent.Default, AutoDetection.No)
{
}
#endregion
#region Control Values and functions
protected AutoDetection m_AutoDetection = AutoDetection.No;
public AutoDetection AutoDetection { get { return m_AutoDetection; } }
protected TextContent m_TextContent = TextContent.Default;
public TextContent TextContent { get { return m_TextContent; } }
protected string m_Prefix = "";
public string Prefix { get { return m_Prefix; } }
protected string RemovePrefix(string value)
{
if (!string.IsNullOrEmpty(Prefix))
value = value.Remove(0, Prefix.Length);
return value;
}
protected string AddPrefix(string value)
{
if (!string.IsNullOrEmpty(Prefix))
value = Prefix + value;
return value;
}
#endregion
#region EnumToString
/// <summary>
/// Enums to string.
/// </summary>
/// <returns>The to string.</returns>
/// <param name="value">Value.</param>
public string EnumToString(T value)
{
string result = value.ToString();
result = RemovePrefix(result);
switch (TextContent)
{
case TextContent.Lower:
result = result.ToLower();
break;
case TextContent.Upper:
result = result.ToUpper();
break;
}
return result;
}
#endregion
#region StringToEnum
protected List<string> Names = null;
protected T[] Values = null;
/// <summary>
/// Strings to enum.
/// </summary>
/// <returns>The to enum.</returns>
/// <param name="value">Value.</param>
public T StringToEnum(string value)
{
T? result = null;
if (AutoDetection != AutoDetection.No)
{
if (Names == null)
{
Names = Enum.GetNames(typeof(T)).ToList();
switch (AutoDetection)
{
case AutoDetection.YesButLower:
Names = Names.Select(x => x.ToLower()).ToList();
break;
case AutoDetection.YesButUpper:
Names = Names.Select(x => x.ToUpper()).ToList();
break;
}
Values = (T[])Enum.GetValues(typeof(T));
}
// no prefix for search because we are searching part of the names
// value= AddPrefix(value)
switch (AutoDetection)
{
case AutoDetection.YesButLower:
value = value.ToLower();
break;
case AutoDetection.YesButUpper:
value = value.ToUpper();
break;
}
for (int index = 0; index < Names.Count; index++)
{
string name = Names[index];
if (name.Contains(value))
{
result = Values[index];
break;
}
}
}
else
{
value = AddPrefix(value);
T outValue;
if (Enum.TryParse(value, out outValue))
result = outValue;
}
if (result == null)
Log.Message(LogType.Message, "Enum not found for value {0}. Choose default enum {1}", value, default(T));
return result ?? default(T);
}
#endregion
#region Hide for the programer
[EditorBrowsable(EditorBrowsableState.Never)]
public override string ToString()
{
return base.ToString();
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment