简体   繁体   English

C# - 如何以某种方式使用枚举标志

[英]C# - How to use enum flags in a certain way

What I am I trying to do is this: 我想做的是这样的:

[Flags]
public enum Actions
{
    Action1 = 0x01,
    Action2 = 0x02,
    Action3 = 0x04
}

The object has the actions flag set to 7 to begin with. 该对象的actions标志设置为7开始。 The object can then perform any of the actions available. 然后,该对象可以执行任何可用的操作。 But here is the kicker. 但这里是踢球者。 The object can perform the actions in this combination: 该对象可以执行以下组合中的操作:

-Action 1, Action 2, Action 3 - 行动1,行动2,行动3

-Action 1, Action 1, Action 3 - 行动1,行动1,行动3

-Action 1, Action 1, Action 2 - 行动1,行动1,行动2

-Action 1, Action 1, Action 1 - 行动1,行动1,行动1

So, actions 2 and 3 can only be used once, while action 1 can be used, upto, three times. 因此,动作2和3只能使用一次,而动作1可以使用,最多三次。 If Action 2 or Action 3 is used, then Action 1 can only be used twice. 如果使用Action 2或Action 3,则Action 1只能使用两次。 Is this the best way to go about this? 这是解决这个问题的最好方法吗? Or should I try to create a new object that will let me handle this? 或者我应该尝试创建一个让我处理这个问题的新对象? I would like to use enums to do this, but I can't, for the life of me, figure out how to do this or find anything on the web regarding something like this. 我想使用枚举来做到这一点,但我不能,为了我的生活,弄清楚如何做到这一点或在网上找到关于这样的事情。

Thank you, in advance, for any help that can be provided. 提前感谢您提供的任何帮助。

The enums only point to a single action. 枚举只指向一个动作。 If you have a combination of actions, I can suggest a collection to organize them. 如果你有一系列动作,我可以建议一个集合来组织它们。

Example: 例:

List<Actions> _actionCombos = new List<Actions>(3);

_actionsCombos.Add(Action.Action1);
_actionsCombos.Add(Action.Action1);
_actionsCombos.Add(Action.Action1);

This would be the combination for all the 1s. 这将是所有1的组合。 It sounds like there is a another variable that determines the action combinations. 听起来有一个另一个变量决定了动作组合。 So, you could build logic in for that value to return the proper action combination (if it exists). 因此,您可以为该值构建逻辑以返回正确的操作组合(如果存在)。

I would like to use enums to do this, but I can't... 我想使用枚举来做到这一点,但我不能......

You can certainly use an enumerated type to define a constant for each action, but you'll need to implement some logic in order to enforce your rules. 您当然可以使用枚举类型为每个操作定义常量,但是您需要实现一些逻辑以强制执行规则。 The enumerated type cannot do that alone. 枚举类型不能单独执行此操作。 You can create a class used to manage these actions that uses the enumerated value to determine the type as a first step, but it will need to maintain some state in order to determine whether or not a given action is valid at a given time. 您可以创建一个用于管理这些操作的类,这些操作使用枚举值来确定类型作为第一步,但是它需要维护某个状态以确定给定操作在给定时间是否有效。

With enum flags you cannot combine (OR) the same value twice and so you cannot perform the kind of validation you're looking for. 使用枚举标志,您无法将相同的值组合(或)两次,因此您无法执行您正在寻找的验证类型。 You will have to create a class or use a collection and apply your rules logic. 您必须创建一个类或使用集合并应用规则逻辑。 Since you have to do this anyway you don't have to worry about a Flags attribute. 因为无论如何你必须这样做,你不必担心Flags属性。 It will not be useful to you. 它对你没用。 Not in this case anyway. 反正不是这种情况。

But, if your cases are limited to the four examples that you list then you could have an enum with four values that represents one of each of the cases. 但是,如果您的案例仅限于您列出的四个示例,那么您可以使用包含四个值的枚举来表示每个案例中的一个。 Bit flagging would still not be useful here but you would have some rules enforcement via an enum. 位标记在这里仍然没有用,但你可以通过枚举实现一些规则。

I enuded doing this to get it to work how I wanted. 我这样做是为了让它按照我想要的方式工作。

I still need to go through and refactor, but it works so far for my need: 我仍然需要经历并重构,但它到目前为止我的需要:

public Actions Actions{get; set;}

public bool UseAction(Actions action)
        {
            bool mReturn = false;

            if (action == Actions.Action2)
            {
                if ((this.Actions & Actions.Action2) == Actions.Action2)
                {
                    mReturn = true;
                    this.Actions = this.Actions & ~action;
                }
                else if ((this.Actions & Actions.Action3) == Actions.Action3)
                {
                    mReturn = true;
                    this.Actions = this.Actions & ~Actions.Action3;
                }
                else
                {
                    mReturn = false;
                }
            }
            else if (action == Actions.Action3)
            {
                if ((this.Actions & Actions.Action3) == Actions.Action3)
                {
                    mReturn = true;
                    this.Actions = this.Actions & ~action;
                }
            }
            else
            {
                if ((this.Actions & Actions.Action1) == Actions.Action1)
                {
                    mReturn = true;
                    this.Actions = this.Actions & ~action;
                }
                else if ((this.Actions & Actions.Action3) == Actions.Action3)
                {
                    mReturn = true;
                    this.Actions = this.Actions & ~Actions.Action3;
                }
                else if ((this.Actions & Actions.Action2) == Actions.Action2)
                {
                    mReturn = true;
                    this.Actions = this.Actions & ~Actions.Action2;
                }
            }

            return mReturn;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM