首页 > 其他 > 详细

Enum 枚举

时间:2014-02-25 22:05:04      阅读:501      评论:0      收藏:0      [点我收藏+]

一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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);
        }
    }
}

  

Enum 枚举

原文:http://www.cnblogs.com/cw_volcano/p/3566340.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!