After investigating I concluded what string Enum with attributes is too slow and inconvenient for my purposes.
So my question - is my implementation normal or way redundant and dangerous?=)
public class StringEnum<TChildType> where TChildType: StringEnum<TChildType>, new()
{
private readonly Type childType;
private string _value;
private readonly HashSet<string> members;
public string Value
{
get { return _value; }
set
{
if (!Contains(value))
{
throw new NotImplementedException(String.Format("Value '{0}' wasnt found in Enum", value));
}
_value = value;
}
}
public IEnumerable<string> Values
{
get { return members; }
}
public bool Contains(string value)
{
return members.Contains(value);
}
public static IEnumerable<string> GetValues()
{
return Service.GetGenericConstProperties<string>(typeof(TChildType));
}
public StringEnum()
{
childType = typeof(TChildType);
members = Service.GetGenericStaticProperties<string>(childType).ToHashSet();
if (members.Count < 2) throw new Exception("Fill Enum!");
}
public static implicit operator StringEnum<TChildType>(string str)
{
return new TChildType { Value = str };
}
public static implicit operator string(StringEnum<TChildType> source)
{
return source != null ? source.Value : null;
}
}
Enum Example:
public class PrinterType : StringEnum<PrinterType>
{
public const string CommonPrinter = "... ...";
.....
}
How to use:
public class MyPrinter
{
public StringEnum<PrinterType> PrintType = PrinterType.CommonPrinter;
}
var list = PrinterType.GetValues().ToList();
var e = new MyPrinter();
var q = e.PrintType;
if (e.PrintType == PrinterType.Ярлыков)
...
if (e.PrintType.Contains("jh"))
or even like backing Field:
private StringEnum<PrinterType> _type;
public string Type {... return _type... }