Beispiel #1
0
        /// <summary>
        /// Retrieves the default value of the enumeration.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns>The default value of the enumeration.</returns>
        /// <remarks>The standard default for an enum is 0, but this method will honor a Attributes.EnumDefaultValueAttribute if one has been applied by the developer of the enum.</remarks>
        public T Default <T>()
        {
            T result;

            EnumDefaultValueAttribute.GetDefaultValue <T>(out result);
            return(result);
        }
Beispiel #2
0
    public static string GetEnumDefaultValueEnum(this object value)
    {
        Type      type  = value.GetType();
        FieldInfo field = type.GetField(Enum.GetName(type, value));

        if (field != null)
        {
            if (Attribute.IsDefined(field, typeof(EnumDefaultValueAttribute)))
            {
                EnumDefaultValueAttribute des = Attribute.GetCustomAttribute(field, typeof(EnumDefaultValueAttribute)) as EnumDefaultValueAttribute;
                if (des != null)
                {
                    return(des.DefaultValue);
                }
            }
        }
        return(value.ToString());
    }