C# 枚举拼写错误更正

本文关键字:错误 枚举 | 更新日期: 2023-09-27 17:55:30

我相信每个人都在开发过程中的某个地方犯了一个错别字,并在发布后意识到了这一点。好吧,我遇到了同样的问题,除了这个问题存在于枚举选项中。

我在 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 . . .

C# 枚举拼写错误更正

Alioza方法的替代答案:

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

在修补产品;)时运行它

这应该有效:

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

编辑:您最安全的选择是使用@Bas的建议。

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

例如,您可以实现一个属性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中具有相同的示例!

试试这个

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

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

class Program
{
    static void Main(string[] args)
    {
        bool result = (SlideShowZoomEffect.ZoonOut == SlideShowZoomEffect.ZoomOut); //True
        Console.WriteLine(result);
        Console.ReadLine();
    }
}