C#enum拼写错误修正

时间:2015-06-10 20:38:29

标签: c# enums

我确信每个人在开发过程中都会在某处发生错字,并在发布后意识到这一点。好吧,我遇到了同样的问题,除了这个问题存在于Enum选项中。

我在C#

中有一个如下所示的枚举定义
    public enum SlideShowZoomEffect
    {
        NoEffect,
        ZoomIn,
        ZoonOut
    }

最近,在产品发布后,我们意识到ZoonOut有一个错字(它应该是ZoomOut)。我们想要更改它,但它将作为字符串保存到数据库中(因此它保存为ZoonOut)并且程序将通过调用enum.parse(...)重新构建枚举,这允许我们在程序中将其用作枚举

这里的问题是,一旦我们将ZoonOut更改为ZoomOut,是否有任何方法可以向后兼容,以便(字符串)ZoonOut和(字符串)ZoomOut将被解析为(枚举)ZoomOut?

任何建议都会很棒!

谢谢

更新(此问题的解决方案):

下面接受了答案,这是我的测试代码,以显示对此接受的解决方案的影响,以供将来参考。谢谢大家

class Program
{
    static void Main(string[] args)
    {
        //Testing input as (string)ZoomOut
        Console.WriteLine("Testing #1: Input String = 'ZoomOut'");
        TestCase("ZoomOut");
        Console.WriteLine("\n\n\n");


        //Testing input as (string)ZoonOut
        Console.WriteLine("Testing #2: Input String = 'ZoonOut'");
        TestCase("ZoonOut");
        Console.WriteLine("\n\n\n");


        //Additional testing to ensure the TestCase function is working, we should see something else detected here
        Console.WriteLine("Testing #3: Input String = 'ZoomIn'");
        TestCase("ZoomIn");
    }


    static void TestCase(string inputString)
    {
        SlideShowZoomEffect enumInput = (SlideShowZoomEffect)Enum.Parse(typeof(SlideShowZoomEffect), inputString);

        Console.WriteLine("enumInput.tostring() = " + enumInput.ToString());


        Console.WriteLine("\n===> using case SlideShowZoomEffect.ZoonOut:");
        switch (enumInput)
        {
            case SlideShowZoomEffect.ZoonOut:
                Console.WriteLine("      ===> ZoomOut detected");
                break;

            default:
                Console.WriteLine("      ===> Something else detected");
                break;
        }



        Console.WriteLine("\n===> using case SlideShowZoomEffect.ZoomOut:");
        switch (enumInput)
        {
            case SlideShowZoomEffect.ZoomOut:
                Console.WriteLine("      ===> ZoomOut detected");
                break;

            default:
                Console.WriteLine("      ===> Something else detected");
                break;
        }
    }


    public enum SlideShowZoomEffect
    {
        NoEffect,
        ZoomIn,
        ZoomOut,
        ZoonOut = ZoomOut
    }
}

这是控制台的输出

Testing #1: Input String = 'ZoomOut'
enumInput.tostring() = ZoomOut

===> using case SlideShowZoomEffect.ZoonOut:
      ===> ZoomOut detected

===> using case SlideShowZoomEffect.ZoomOut:
      ===> ZoomOut detected




Testing #2: Input String = 'ZoonOut'
enumInput.tostring() = ZoomOut

===> using case SlideShowZoomEffect.ZoonOut:
      ===> ZoomOut detected

===> using case SlideShowZoomEffect.ZoomOut:
      ===> ZoomOut detected




Testing #3: Input String = 'ZoomIn'
enumInput.tostring() = ZoomIn

===> using case SlideShowZoomEffect.ZoonOut:
      ===> Something else detected

===> using case SlideShowZoomEffect.ZoomOut:
      ===> Something else detected
Press any key to continue . . .

4 个答案:

答案 0 :(得分:9)

Alioza方法的替代答案:

update TableX set MyEnumField = 'ZoomOut' where MyEnumField = 'ZoonOut'

修补产品时运行它;)

答案 1 :(得分:5)

这应该有效:

  public enum SlideShowZoomEffect
    {
        NoEffect,
        ZoomIn,
        ZoomOut,
        ZoonOut = ZoomOut

    }

编辑:你最安全的赌注是使用@Bas的建议。

答案 2 :(得分:1)

我相信你最好的选择应该是使用枚举值的属性。

例如,您可以实现属性SynonymAttribute

public class SynonymAttribute : Attribute
{
    private readonly string _name;
    public SynonymAttribute(string name)
    {
        _name = name;
    }

    public string Name
    {
        get
        {
            return _name;
        }
    }
}

然后,只要您想将同义词映射到某个现有的枚举值,就应该使用SynonymAttribute

public enum SlideShowZoomEffect 
{
     NoEffect,
     ZoomIn,

     [Synonym("ZoonOut")]
     ZoomOut
}

最后,您可以实现一些这样的帮助方法来解决您的问题:

public static class EnumHelper
{
    public static bool TryParse<TEnum>(string nameOrSynonym, out TEnum enumValue)where TEnum : struct
    {
        enumValue = default (TEnum);
        bool success = false;
        TEnum result;

        // First of all, this is the first attemp to parse the enum
        // value using regular Enum.TryParse. If it succeeds, it will mean
        // that passed enum value name is already in the enumeration. 
        if (!Enum.TryParse<TEnum>(nameOrSynonym, true, out result))
        {
            nameOrSynonym = nameOrSynonym.ToLowerInvariant();

            // If we need to look for a synonym of passed enumeration value
            // name, then we need reflection to look for static fields
            // in the given enumeration type. Enumeration values are just
            // static fields.

            // The SingleOrDefault part will look for fields with 
            // SynonymAttribute and it will extract the one with the
            // synonym which equals the passed enumeration value name!
            FieldInfo enumValueField = typeof (TEnum).GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.GetField)
                                                    .SingleOrDefault
                                                    (
                                                        field => field.GetCustomAttribute<SynonymAttribute>() != null 
                                                                && field.GetCustomAttribute<SynonymAttribute>().Name.ToLowerInvariant() == nameOrSynonym
                                                    );



            // If the synonym was found, then we get the field value
            // and we set it to the result enum value!
            if (enumValueField != null)
            {
                enumValue = (TEnum)enumValueField.GetValue(null);
                success = true;
            }
        }
        else
        {
            enumValue = result;
            success = true;
        }

        return success;
    }
}

以下是具有同义词支持的EnumHelper.TryParse的示例用法:

    SlideShowZoomEffect enumValue;

    EnumHelper.TryParse<SlideShowZoomEffect>("ZoonOut", out enumValue);

    // This will output "ZoomOut"!
    Console.WriteLine("{0}", enumValue.ToString("f"));

另外,DotNetFiddle中的相同样本!

答案 3 :(得分:0)

试试这个

findObjectsInBackgroundWithBlock

我在简单的控制台程序中尝试过它。它表明它们是相同的。

public enum SlideShowZoomEffect
{
    NoEffect = 1,
    ZoomIn = 2,
    ZoonOut = 3,
    ZoomOut = 3
}