Skip to content

Instantly share code, notes, and snippets.

@Eibwen
Last active December 14, 2015 01:59
Show Gist options
  • Save Eibwen/5010357 to your computer and use it in GitHub Desktop.
Save Eibwen/5010357 to your computer and use it in GitHub Desktop.
type-safe-enum pattern Started from http://stackoverflow.com/a/424414/356218 then MSDN operators...
void Main()
{
ParamNames.TESTVALUE.Dump();
ParamNames.TEST.Dump();
ParamNames p1 = ((ParamNames)ParamNames.TESTVALUE).Dump();
//Not possible:
//ParamNames p2 = (ParamNames.TESTVALUE as ParamNames);
bool t3 = (p1 == ParamNames.TEST).Dump();
bool t4 = (p1 == (ParamNames)ParamNames.TESTVALUE).Dump();
bool t5 = (p1 == ParamNames.TESTVALUE).Dump();
bool t6 = (ParamNames.TESTVALUE == p1).Dump();
bool t7 = (ParamNames.TEST == "FAILTEST").Dump();
bool t8 = ("FAILTEST" == ParamNames.TEST).Dump();
ParamNames p3 = ((ParamNames)"FAILTEST").Dump();
// //NOT POSSIBLE
// //Might be possible, if can implicity convert to string
// switch ("FAILTEST")
// {
// case ParamNames.TEST:
// "Success".Dump();
// break;
// default:
// "Miss".Dump();
// break;
// }
// //NOT POSSIBLE... here is where const values would win...
// switch (p1)
// {
// case ParamNames.TEST:
// "Success".Dump();
// break;
// default:
// "Miss".Dump();
// break;
// }
}
public sealed class ParamNames
{
//Needs to be above the c'tor
private static readonly Dictionary<string, ParamNames> instance = new Dictionary<string,ParamNames>();
private readonly String name;
public const string TESTVALUE = "TESTVALUEFORTESTING";
public static readonly ParamNames TEST = new ParamNames (TESTVALUE);
public static readonly ParamNames WINDOWSAUTHENTICATION = new ParamNames ("WINDOWS");
public static readonly ParamNames SINGLESIGNON = new ParamNames ("SSN");
//public static readonly ParamNames DUPETEST = new ParamNames ("SSN");
static ParamNames()
{
try
{
//Test for duplicates
var test = typeof(ParamNames).GetFields(BindingFlags.Public | BindingFlags.Static)
.Where(fi => fi.IsInitOnly)
.ToDictionary(k => k.GetValue(null), v => v.Name);
test.Count.Dump();
}
catch (ArgumentException ex)
{
throw new Exception("Duplicate ParamName Values are not allowed!", ex);
}
}
private ParamNames(String name){
this.name = name;
instance[name] = this;
}
public override String ToString(){
return name;
}
public static implicit operator ParamNames(string str)
{
ParamNames result;
if (instance.TryGetValue(str, out result))
return result;
else
return null;
//throw new InvalidCastException();
}
// public static implicit operator String(ParamNames pn)
// {
// return pn.ToString();
// }
public override bool Equals(System.Object obj)
{
// If parameter is null return false.
if (obj == null)
{
return false;
}
// // If parameter cannot be cast to Point return false.
// ParamNames p = obj as ParamNames;
// if ((System.Object)p == null)
// {
// "AS FAILS".Dump();
// }
ParamNames p = (ParamNames)obj;
if ((object)p == null)
{
return false;
}
// Return true if the fields match:
return (name == p.name);
}
public bool Equals(ParamNames p)
{
// If parameter is null return false:
if ((object)p == null)
{
return false;
}
// Return true if the fields match:
return (name == p.name);
}
public override int GetHashCode()
{
return name.GetHashCode();
}
public static bool operator ==(ParamNames a, ParamNames b)
{
// If both are null, or both are same instance, return true.
if (System.Object.ReferenceEquals(a, b))
{
return true;
}
// If one is null, but not both, return false.
// Cast to object to avoid recursion
if (((object)a == null) || ((object)b == null))
{
return false;
}
// Return true if the fields match:
return a.name == b.name;
}
public static bool operator !=(ParamNames a, ParamNames b)
{
return !(a == b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment