Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public enum AuthenticationMethod
- {
- FORMS = 1,
- WINDOWSAUTHENTICATION = 2,
- SINGLESIGNON = 3
- }
- public class StringValue : System.Attribute
- {
- private string _value;
- public StringValue(string value)
- {
- _value = value;
- }
- public string Value
- {
- get { return _value; }
- }
- }
- public enum AuthenticationMethod
- {
- [StringValue("FORMS")]
- FORMS = 1,
- [StringValue("WINDOWS")]
- WINDOWSAUTHENTICATION = 2,
- [StringValue("SSO")]
- SINGLESIGNON = 3
- }
- public static class StringEnum
- {
- public static string GetStringValue(Enum value)
- {
- string output = null;
- Type type = value.GetType();
- //Check first in our cached results...
- //Look for our 'StringValueAttribute'
- //in the field's custom attributes
- FieldInfo fi = type.GetField(value.ToString());
- StringValue[] attrs =
- fi.GetCustomAttributes(typeof(StringValue),
- false) as StringValue[];
- if (attrs.Length > 0)
- {
- output = attrs[0].Value;
- }
- return output;
- }
- }
- string valueOfAuthenticationMethod = StringEnum.GetStringValue(AuthenticationMethod.FORMS);
- public sealed class AuthenticationMethod {
- private readonly String name;
- private readonly int value;
- public static readonly AuthenticationMethod FORMS = new AuthenticationMethod (1, "FORMS");
- public static readonly AuthenticationMethod WINDOWSAUTHENTICATION = new AuthenticationMethod (2, "WINDOWS");
- public static readonly AuthenticationMethod SINGLESIGNON = new AuthenticationMethod (3, "SSN");
- private AuthenticationMethod(int value, String name){
- this.name = name;
- this.value = value;
- }
- public override String ToString(){
- return name;
- }
- }
- private static readonly Dictionary<string, AuthenticationMethod> instance
- instance[name] = value;
- public static explicit operator AuthenticationMethod(string str)
- {
- AuthenticationMethod result;
- if (instance.TryGet(str, out result))
- return result
- else
- throw new InvalidCastException();
- }
- Enum.GetName(Type MyEnumType, object enumvariable)
- Shipper x = Shipper.FederalExpress;
- string s = Enum.GetName(typeof(Shipper), x);
- Console.WriteLine("Auth method: {0}", AuthenticationMethod.Forms.ToString());
- static string ToFriendlyCase(this string EnumString)
- {
- return Regex.Replace(EnumString, "(?!^)([A-Z])", " $1");
- }
- Console.WriteLine("ConvertMyCrazyPascalCaseSentenceToFriendlyCase".ToFriendlyCase());
- public static string GetStringValue(this AuthenticationMethod value)
- {
- string output = null;
- Type type = value.GetType();
- FieldInfo fi = type.GetField(value.ToString());
- StringValue[] attrs = fi.GetCustomAttributes(typeof(StringValue), false) as StringValue[];
- if (attrs.Length > 0)
- output = attrs[0].Value;
- return output;
- }
- Console.WriteLine(AuthenticationMethod.SSO.GetStringValue());
- public static string GetName( this MyEnum input ) {
- switch ( input ) {
- case MyEnum.WINDOWSAUTHENTICATION:
- return "Windows";
- //and so on
- }
- }
- public static string GetDescription<T>(this object enumerationValue)
- where T : struct
- {
- Type type = enumerationValue.GetType();
- if (!type.IsEnum)
- {
- throw new ArgumentException("EnumerationValue must be of Enum type", "enumerationValue");
- }
- //Tries to find a DescriptionAttribute for a potential friendly name
- //for the enum
- MemberInfo[] memberInfo = type.GetMember(enumerationValue.ToString());
- if (memberInfo != null && memberInfo.Length > 0)
- {
- object[] attrs = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
- if (attrs != null && attrs.Length > 0)
- {
- //Pull out the description value
- return ((DescriptionAttribute)attrs[0]).Description;
- }
- }
- //If we have no description attribute, just return the ToString of the enum
- return enumerationValue.ToString();
- }
- public enum Cycle : int
- {
- [Description("Daily Cycle")]
- Daily = 1,
- Weekly,
- Monthly
- }
- public static string ToSimpleString(this enum)
- {
- switch (enum)
- {
- case ComplexForms:
- return "ComplexForms";
- break;
- }
- }
- public static string ToFormattedString(this enum)
- {
- switch (enum)
- {
- case ComplexForms:
- return "Complex Forms";
- break;
- }
- }
- [TypeConverter(typeof(CustomEnumTypeConverter(typeof(MyEnum))]
- public enum MyEnum
- {
- // The custom type converter will use the description attribute
- [Description("A custom description")]
- ValueWithCustomDescription,
- // This will be exposed exactly.
- Exact
- }
- public class CustomEnumTypeConverter<T> : EnumConverter
- where T : struct
- {
- private static readonly Dictionary<T,string> s_toString =
- new Dictionary<T, string>();
- private static readonly Dictionary<string, T> s_toValue =
- new Dictionary<string, T>();
- private static bool s_isInitialized;
- static CustomEnumTypeConverter()
- {
- System.Diagnostics.Debug.Assert(typeof(T).IsEnum,
- "The custom enum class must be used with an enum type.");
- }
- public CustomEnumTypeConverter() : base(typeof(T))
- {
- if (!s_isInitialized)
- {
- Initialize();
- s_isInitialized = true;
- }
- }
- protected void Initialize()
- {
- foreach (T item in Enum.GetValues(typeof(T)))
- {
- string description = GetDescription(item);
- s_toString[item] = description;
- s_toValue[description] = item;
- }
- }
- private static string GetDescription(T optionValue)
- {
- var optionDescription = optionValue.ToString();
- var optionInfo = typeof(T).GetField(optionDescription);
- if (Attribute.IsDefined(optionInfo, typeof(DescriptionAttribute)))
- {
- var attribute =
- (DescriptionAttribute)Attribute.
- GetCustomAttribute(optionInfo, typeof(DescriptionAttribute));
- return attribute.Description;
- }
- return optionDescription;
- }
- public override object ConvertTo(ITypeDescriptorContext context,
- System.Globalization.CultureInfo culture,
- object value, Type destinationType)
- {
- var optionValue = (T)value;
- if (destinationType == typeof(string) &&
- s_toString.ContainsKey(optionValue))
- {
- return s_toString[optionValue];
- }
- return base.ConvertTo(context, culture, value, destinationType);
- }
- public override object ConvertFrom(ITypeDescriptorContext context,
- System.Globalization.CultureInfo culture, object value)
- {
- var stringValue = value as string;
- if (!string.IsNullOrEmpty(stringValue) && s_toValue.ContainsKey(stringValue))
- {
- return s_toValue[stringValue];
- }
- return base.ConvertFrom(context, culture, value);
- }
- }
- [TypeConverter(typeof(CustomEnumTypeConverter(typeof(MyEnum))]
- public enum MyEnum
- {
- // The custom type converter will use the description attribute
- [Description("A custom description")]
- ValueWithCustomDescription,
- // This will be exposed exactly.
- Exact
- }
- [TypeConverter(typeof(CustomEnumTypeConverter<MyEnum>))]
- public enum MyEnum
- {
- // The custom type converter will use the description attribute
- [Description("A custom description")]
- ValueWithCustomDescription,
- // This will be exposed exactly.
- Exact
- }
- public sealed class FormsAuth
- {
- public override string ToString{return "Forms Authtentication";}
- }
- public sealed class WindowsAuth
- {
- public override string ToString{return "Windows Authtentication";}
- }
- public sealed class SsoAuth
- {
- public override string ToString{return "SSO";}
- }
- object auth = new SsoAuth(); //or whatever
- //...
- //...
- // blablabla
- DoSomethingWithTheAuth(auth.ToString());
- public enum AuthenticationMethod
- {
- FORMS = 1,
- WINDOWSAUTHENTICATION = 2,
- SINGLESIGNON = 3
- }
- public class MyClass
- {
- private Dictionary<AuthenticationMethod, String> map = new Dictionary<AuthenticationMethod, String>();
- public MyClass()
- {
- map.Add(AuthenticationMethod.FORMS,"Forms Authentication");
- map.Add(AuthenticationMethod.WINDOWSAUTHENTICATION ,"Windows Authentication");
- map.Add(AuthenticationMethod.SINGLESIGNON ,"SSo Authentication");
- }
- }
- /// <summary>
- /// An Enum-like object which carries the string flags used in InternetSecure payment forms.
- /// </summary>
- [Serializable]
- public sealed class PaymentItemFlag : StringEnum
- {
- private static readonly PaymentItemFlag _testSuccess = new PaymentItemFlag(1, "{TEST}");
- private static readonly PaymentItemFlag _testDecline = new PaymentItemFlag(2, "{TESTD}");
- private static readonly PaymentItemFlag _recurringBilling = new PaymentItemFlag(3, "{RB}");
- public static PaymentItemFlag TestSuccess { get { return _testSuccess; } }
- public static PaymentItemFlag TestDecline { get { return _testDecline; } }
- public static PaymentItemFlag RecurringBilling { get { return _recurringBilling; } }
- private PaymentItemFlag(int value, string name)
- : base(value, name)
- { }
- public static implicit operator PaymentItemFlag(string x)
- {
- switch (x)
- {
- case "{TEST}":
- return PaymentItemFlag.TestSuccess;
- case "{TESTD}":
- return PaymentItemFlag.TestDecline;
- case "{RB}":
- return PaymentItemFlag.RecurringBilling;
- default:
- return default(PaymentItemFlag);
- }
- }
- public static implicit operator PaymentItemFlag(int x)
- {
- switch (x)
- {
- case 1:
- return PaymentItemFlag.TestSuccess;
- case 2:
- return PaymentItemFlag.TestDecline;
- case 3:
- return PaymentItemFlag.RecurringBilling;
- default:
- return default(PaymentItemFlag);
- }
- }
- }
- /// <summary>
- /// Base class for Enum-like strings
- ///
- /// Includes equality plumbing in case you're dealing in copies of StringEnums.
- /// </summary>
- [Serializable]
- public abstract class StringEnum : Object
- {
- protected readonly string _name;
- protected readonly int _value;
- protected StringEnum(int value, string name)
- {
- _name = name;
- _value = value;
- }
- public override string ToString()
- {
- return _name;
- }
- public static implicit operator string(StringEnum x)
- {
- return x._name;
- }
- public static implicit operator int(StringEnum x)
- {
- return x._value;
- }
- public static bool operator ==(StringEnum x, StringEnum y)
- {
- if (((object)x == null) || ((object)y == null))
- {
- return false;
- }
- if (Object.ReferenceEquals(x, y))
- {
- return true;
- }
- return (x._value == y._value);
- }
- public static bool operator !=(StringEnum x, StringEnum y)
- {
- return !(x._value == y._value);
- }
- public override bool Equals(object obj)
- {
- StringEnum p = obj as StringEnum;
- if ((object)p == null)
- {
- return false;
- }
- return (_value == p._value);
- }
- public bool Equals(StringEnum flag)
- {
- return (_value == flag._value);
- }
- public override int GetHashCode()
- {
- return _value.GetHashCode();
- }
- }
- PaymentItemFlag fas = "{asdf}"; // fas will be null
- PaymentItemFlag asd = "{TESTD}"; // asd will be PaymentItemFlag.TestDecline
- bool equal = (asd == PaymentItemFlag.TestSuccess); // equal will be false
- bool equal2 = (asd == PaymentItemFlag.TestDecline); // equal2 will be true
- HashSet<PaymentItemFlag> set = new HashSet<PaymentItemFlag>();
- set.Add("{asdf}"); // null
- set.Add(1); // PaymentItemFlag.TestSuccess
- set.Add("{TESTD}"); // PaymentItemFlag.TestDecline
- foreach (string str in Enum.GetNames(typeof(enumHeaderField)))
- {
- Debug.WriteLine(str);
- }
- public Type ResourceType { get; private set; }
- public string ResourceName { get; private set; }
- public int SortOrder { get; private set; }
- public EnumResourceAttribute(Type ResourceType,
- string ResourceName,
- int SortOrder)
- {
- this.ResourceType = ResourceType;
- this.ResourceName = ResourceName;
- this.SortOrder = SortOrder;
- }
- }
- }
- return string.Empty;
- }
- public static IList GetStrings(this Type enumType)
- {
- List<string> stringList = new List<string>();
- FieldInfo[] fiArray = enumType.GetFields();
- foreach (FieldInfo fi in fiArray)
- {
- EnumResourceAttribute ea =
- (EnumResourceAttribute)fi
- .GetCustomAttributes(typeof(EnumResourceAttribute), false)
- .FirstOrDefault();
- if (ea != null)
- {
- PropertyInfo pi = ea.ResourceType
- .GetProperty(CommonConstants.ResourceManager);
- if (pi != null)
- {
- ResourceManager rm = (ResourceManager)pi
- .GetValue(null, null);
- stringList.Add(rm.GetString(ea.ResourceName));
- }
- }
- }
- return stringList.ToList();
- }
- }
Add Comment
Please, Sign In to add comment