CSharp · Console
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var q = Parse<TestType>((Test.ACT | Test.SAT_2016).ToString());
Console.WriteLine(string.Format("Type: {0}\t Value: {1}", q.GetType(), q));
// */
}
public static T Parse<T>(string value, bool ignoreCase = false)
where T : FlagsAttribute , IComparable, IFormattable, IConvertible
{
if (value.ToString().Contains(",")){
var parts = value.ToString().Split(',');
var results = new List<T> ();
var final = default(T);
foreach (var part in parts){
try {
final |= (T)Enum.Parse(typeof(T), part, ignoreCase) ;
}catch (Exception e){
Console.WriteLine("Got exception");
/* Supressed */
}
}
return final;
}else{
return (T) Enum.Parse(typeof(T), value.ToString(), ignoreCase);
}
}
[FlagsAttribute]
public enum Test{
ACT = 32,
SAT_2016 = 65536,
};
[FlagsAttribute]
public enum TestType {
ACT = 4,
SAT_2016 = 39,
};
}