我如何简化这条条件语句的长链? [英] How could I simplify this long chain of conditional statements?

查看:77
本文介绍了我如何简化这条条件语句的长链?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下枚举

[Flags]
internal enum DataSectionFlags : uint
{
    TypeReg = 0x0,
    TypeDsect = 0x01,
    TypeNoLoad = 0x02,
    TypeGroup = 0x04,
    TypeNoPadded = 0x08,
    TypeCopy = 0x010,

    ContentCode = 0x020,
    ContentInitializedData = 0x040,
    ContentUninitializedData = 0x080,

    LinkOther = 0x0100,
    LinkInfo = 0x0200,

    TypeOver = 0x0400,

    LinkRemove = 0x0800,
    LinkComDat = 0x01000,

    NoDeferSpecExceptions = 0x04000,

    RelativeGP = 0x08000,

    MemPurgeable = 0x020000,

    Memory16Bit = 0x020000,
    MemoryLocked = 0x040000,
    MemoryPreload = 0x080000,

    Align1Bytes = 0x0100000,
    Align2Bytes = 0x0200000,
    Align4Bytes = 0x0300000,
    Align8Bytes = 0x0400000,
    Align16Bytes = 0x0500000,
    Align32Bytes = 0x0600000,
    Align64Bytes = 0x0700000,
    Align128Bytes = 0x0800000,
    Align256Bytes = 0x0900000,
    Align512Bytes = 0x0A00000,
    Align1024Bytes = 0x0B00000,
    Align2048Bytes = 0x0C00000,
    Align4096Bytes = 0x0D00000,
    Align8192Bytes = 0x0E00000,

    LinkExtendedRelocationOverflow = 0x01000000,

    MemoryDiscardable = 0x02000000,
    MemoryNotCached = 0x04000000,
    MemoryNotPaged = 0x08000000,
    MemoryShared = 0x10000000,
    MemoryExecute = 0x20000000,
    MemoryRead = 0x40000000,
    MemoryWrite = 0x80000000
}

我正在像这样用这个枚举强制转换一个uint变量

I am casting a uint variable with this enum like so

var testVariable = (DataSectionFlags) 1610612768;

我有一个像上面这样处理上述变量的方法

I have a method that processes the above variable like this

private static uint GetSectionProtection(DataSectionFlags characteristics)
{
    uint result = 0;

    if (characteristics.HasFlag(DataSectionFlags.MemoryNotCached))
    {
        // PageNoCache

        result |= 0x200;
    }

    if (characteristics.HasFlag(DataSectionFlags.MemoryExecute))
    {
        if (characteristics.HasFlag(DataSectionFlags.MemoryRead))
        {
            if (characteristics.HasFlag(DataSectionFlags.MemoryWrite))
            {
                // PageExecuteReadWrite

                result |= 0x40;
            }

            else
            { 
                // PageExecuteRead

                result |= 0x20;
            }

        }

        else if (characteristics.HasFlag(DataSectionFlags.MemoryWrite))
        {
            // PageExecuteWriteCopy

            result |= 0x80;
        }

        else
        {
            // PageExecute

            result |= 0x10;
        }
    }

    else if (characteristics.HasFlag(DataSectionFlags.MemoryRead))
    {
        if (characteristics.HasFlag(DataSectionFlags.MemoryWrite))
        {
            // PageReadWrite

            result |= 0x04;
        }

        else
        {
            // PageReadOnly

            result |= 0x02;
        }               
    }

    else if (characteristics.HasFlag(DataSectionFlags.MemoryWrite))
    {
        // PageWriteCopy

        result |= 0x08;
    }

    else
    {
        // PageNoAccess

        result |= 0x01;
    }

    return result;
}

我正在尝试简化此方法中的条件语句的长链,但是这样做很麻烦.

I'm attempting to simplify the long chain of conditional statements inside this method but am having trouble doing so.

在保持其功能的同时在方法内部编写条件语句的最简单方法是什么?

What would be the simplest way to write the conditional statements inside the method whilst still maintaining their functionality?

推荐答案

我建议使用类似以下的查找字典:

I suggest a lookup dictionary like:

var sectionProtection = new Dictionary<DataSectionFlags, uint>
{
    [DataSectionFlags.TypeReg ] = 1,
    [DataSectionFlags.TypeDsect ] = 2,
    ...
    [DataSectionFlags.MemoryExecute | DataSectionFlags.MemoryRead | DataSectionFlags.MemoryWrite] = 0x40,
    ...
};

请注意,每种标志组合都需要一个单独的条目.在这种情况下,您可以使用以下语句替换您的函数

Note you'll need a separate entry for every combination of flags. Given such, you can then replace your function with the statement

var testVariable = sectionProtection[(DataSectionFlags) 1610612768];

,或者,如果没有定义每个组合,则:

or, if not every combination is defined:

if (sectionProtection.TryGetValue((DataSectionFlags) 1610612768, out testVariable ))

我认为这不仅更易于理解,运行更快,而且更正确.在创建if ... else if ... else if ...语句列表时,很容易错过组合,使相同的组合返回不同的值或两次包含相同的组合.如果错过了查找字典中的组合,则会出现异常(或TryGetValue返回false).如果将相同的组合两次添加到字典中,则会出现错误.

I consider this not only simpler to understand, faster to run, but also more correct. It is too easy to miss a combination, to have the same combination return different values, or to include the same combination twice when creating a list of if ... else if ... else if ... statements. If you miss a combination in a lookup dictionary you get an exception (or TryGetValue returns false). If you add the same combination to a dictionary twice you get an error.

这篇关于我如何简化这条条件语句的长链?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆