English 中文(简体)
如何改变定义而不影响C#中的客户
原标题:How to change enum definition without impacting clients using it in C#
  • 时间:2010-06-14 12:22:27
  •  标签:
  • c#
  • c#-3.0

我有以下定义。 我已经强调这一点,因为在伐木中使用这种 en,而且我不想通过使用习俗属性来引起思考。 我们使用大量伐木。 现在的要求是将“失窃——InvalidAttempt1”改为“失窃未遂1”。 如果一概改变这一基数,就必须改变其在整个应用中的价值。 我可以用“SP”伐木中的一个空间加以强调。 是否有任何办法可以改变这种状况,而不影响整个适用。 建议。

public enum ActionType
{
    None,
    Created,
    Modified,
    Activated,
    Inactivated,
    Deleted,
    Login,
    Logout,
    ChangePassword,
    ResetPassword,
    InvalidPassword,
    LoginFailed_LockedAccount,
    LoginFailed_InActiveAccount,
    LoginFailed_ExpiredAccount,
    ForgotPassword,
    LoginFailed_LockedAccount_InvalidAttempts,
    LoginFailed_InvalidAttempt1,
    LoginFailed_InvalidAttempt2,
    LoginFailed_InvalidAttempt3,
    ForgotPassword_InvalidAttempt1,
    ForgotPassword_InvalidAttempt2,
    ForgotPassword_InvalidAttempt3,
    SessionTimeOut,
    ForgotPassword_LockedAccount,
    LockedAccount,
    ReLogin,
    ChangePassword_Due_To_Expiration,
    ChangePassword_AutoExpired

}
最佳回答

这样做的最佳方式是说明。 我的理解是,你没有进行思考,但你总是能够取得结果,只有一次才会发生?

添加说明:

[Description("LoginFailed Attempt1")]
LoginFailed_InvalidAttempt1

然后,在显示该表的案文价值时,你可以采用以下代码表示:

private static Dictionary<Type, Dictionary<Enum, string>> enumMaps = null;

public static string GetDescription(Enum value)
{
    Type eType = value.GetType();
    if (enumMaps == null)
    {
        enumMaps = new Dictionary<Type, Dictionary<Enum, string>> ();
    }
    Dictionary<Enum, string> map;
    if (enumMaps.ContainsKey(eType))
    {
        map = enumMaps[eType];
    }
    else
    {
        map = new Dictionary<Enum, string>();
        foreach (Enum e in Enum.GetValues(eType))
        {
            FieldInfo fi = eType.GetField(e.ToString());
            DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(
                typeof(DescriptionAttribute), false);
            map[e] = (attributes.Length > 0) ? attributes[0].Description : e.ToString();
        }
        enumMaps[eType] = map;
    }
    return map[value];
}

如你从上述法典中看到的那样,这种思考只是ce。 其后任何要求相同Enum价值的电话,都将从快速照明的<代码>Dictionary中回收。

问题回答

您可以得出两种总价值相同的分类编号,标明一个过时:

public enum MyEnum {
    Value1,
    Value2,

    [Obsolete]
    Value3,

    NewValue3 = Value3,
}

然后MyEnum.Value3= MyEnum. NewValue3





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签