Skip to content

Instantly share code, notes, and snippets.

@EifelMono
Last active August 29, 2015 13:57
Show Gist options
  • Save EifelMono/9714347 to your computer and use it in GitHub Desktop.
Save EifelMono/9714347 to your computer and use it in GitHub Desktop.
IndexedProperty
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 EifelMonoException("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
}
}
using System;
//using NUnit.Framework;
//using EifelMono.Device;
//using MonoTouch.UIKit;
//using MonoTouch.NUnit.UI;
//using EifelMono.Core;
//using System.Collections.Generic;
//
//namespace LibTest
//{
// public class TestClassIndexAsInt
// {
// public List<string> StringList = new List<string>();
// public IndexedProperty<int, string> ByIndex = null;
//
// public TestClassIndexAsInt()
// {
// ByIndex = new IndexedProperty<int, string>(
// (index) =>
// {
// return StringList[index];
// },
// (index, value) =>
// {
// StringList[index] = value;
// }
// );
// }
// }
//
// public class TestClassIndexAsString
// {
// public List<string> StringList = new List<string>();
// public IndexedProperty<string, string> ByIndex = null;
//
// public TestClassIndexAsString()
// {
// ByIndex = new IndexedProperty<string, string>(
// (index) =>
// {
// return StringList[Convert.ToInt16(index)];
// },
// (index, value) =>
// {
// StringList[Convert.ToInt16(index)] = value;
// }
// );
// }
// }
//
// [TestFixture]
// public class TestIndexedProperty
// {
// private UIView GetView()
// {
// return UnitTestAppDelegate.RootViewController.View;
// }
//
// [Test]
// public void TestAll()
// {
// TestClassIndexAsInt testInt = new TestClassIndexAsInt();
// for (int index = 0; index < 100; index++)
// testInt.StringList.Add(index.ToString());
// for (int index = 0; index < testInt.StringList.Count; index++)
// Assert.True(testInt.StringList[index] == testInt.ByIndex[index]);
// TestClassIndexAsString testString = new TestClassIndexAsString();
// for (int index = 0; index < 100; index++)
// testString.StringList.Add(index.ToString());
// for (int index = 0; index < testString.StringList.Count; index++)
// Assert.True(testString.StringList[index] == testString.ByIndex[index.ToString()]);
//
// }
// }
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment