Enum 枚举

一:

1.
foreach (int val in Enum.GetValues(typeof(AppEnum.HarbourStatus)))
{

          ddlStatus.Items.Add(new ListItem(Enum.GetName(typeof(AppEnum.HarbourStatus), val), val.ToString()));

}

ddlStatus.Items.FindByText(AppEnum.HarbourStatus.Avtal.ToString()).Selected = true;

2.
public enum HarbourStatus
{
            Ansökan = 1,
            Avtal = 2,
            Makulerad = 3,
            Köplats = 4
}

  

二:

 

var errorMes = respCode < 0 ? ((ProcessorStatus)respCode).ToDisplayString() : ((RedemptionStatus)respCode).ToDisplayString();

using System;
using System.Reflection;
using System.Resources;

namespace BallyACSCProvider.Util
{
    internal enum ProcessorStatus : int
    {
        [DisplayString("Certificate Error")]
        CertError = -001,  //certificate error

        [DisplayString("Retry Time out")]
        RetryOut = -002, //exceed retry time

        [DisplayString("Response Error")]
        ResponseError = -003, //error when get response

        NoResponse = -004, // no response need to be checked

        NoResponseCode=-005,  //Has response, but no response code in response

        ResponseOk=0, //
    }

    internal enum StartUpStatus : int
    {
        [DisplayString("Success")]
        Success = 000,

        [DisplayString("Invalid Version")]
        InvalidVersion = 001,

        [DisplayString("Invalid Release")]
        InvalidRelease = 002,

        [DisplayString("Invalid Corporate ID")]
        InvalidCorpID = 003,

        [DisplayString("Invalid Poroperty ID")]
        InvalidPropID = 004,

        [DisplayString("Unknown Kiosk")]
        UnknownKiosk = 005,
    }

    internal enum ValidationStatus : int
    {
        [DisplayString("Accepted")]
        Accepted = 001,

        [DisplayString("Validate declined")]
        Declined = 002,
    }

    internal enum RedemptionStatus : int
    {
        [DisplayString("Accepted")]
        Accepted = 001,

        [DisplayString("Redemption declined")]
        Declined = 002,
    }

    internal enum ProviderStatus : int
    {
        Startup = 1,
        Terminated = 2,
    }

    /// <summary>
    /// Apply to properties or enum that are used in display description string.
    /// </summary>
    [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)]
    internal class DisplayStringAttribute : Attribute
    {
        internal DisplayStringAttribute()
        { }

        internal DisplayStringAttribute(string value)
        {
            _value = value;
        }

        private readonly string _value;
        internal string Value
        {
            get
            {
                return _value;
            }
        }

        internal string ResourceKey { get; set; }
    }

    internal static class EnumExtensions
    {
        internal static string ToDisplayString(this System.Enum en)
        {
            Type type = en.GetType();
            MemberInfo[] memInfo = type.GetMember(en.ToString());

            if (memInfo.Length > 0)
            {
                object[] attrs = memInfo[0].GetCustomAttributes(typeof(DisplayStringAttribute), false);

                if (attrs.Length > 0)
                {
                    var attr = ((DisplayStringAttribute)attrs[0]);
                    return !string.IsNullOrEmpty(attr.ResourceKey)
                        ? GetStringByResourceKey(type, attr.ResourceKey)
                        : attr.Value;
                }
            }

            return en.ToString();
        }

        private static string GetStringByResourceKey(Type enumType, string key)
        {
            if (string.IsNullOrEmpty(key))
                return string.Empty;

            var rm = new ResourceManager(enumType);
            return rm.GetString(key);
        }
    }
}

  

posted on 2014-02-25 11:37  cw_volcano  阅读(311)  评论(0编辑  收藏  举报