在一系列值上执行操作的C#模式是什么?

时间:2010-08-11 18:53:04

标签: c#

我有这段代码:

if (PsionTeklogix.Keyboard.Keyboard.GetModifierKeyState(Key.Orange) == KeyState.Lock)
    PsionTeklogix.Keyboard.Keyboard.InjectKeyboardCommand(Function.Orange, 0, 0);

if (PsionTeklogix.Keyboard.Keyboard.GetModifierKeyState(Key.Blue) == KeyState.Lock)
    PsionTeklogix.Keyboard.Keyboard.InjectKeyboardCommand(Function.Blue, 0, 0);

if (PsionTeklogix.Keyboard.Keyboard.GetModifierKeyState(Key.Shift) == KeyState.Lock)
    PsionTeklogix.Keyboard.Keyboard.InjectKeyboardCommand(Function.Shift, 0, 0);

if (PsionTeklogix.Keyboard.Keyboard.GetModifierKeyState(Key.Control) == KeyState.Lock)
    PsionTeklogix.Keyboard.Keyboard.InjectKeyboardCommand(Function.Control, 0, 0);

...

我想重构分离键/函数定义的代码 动作。 Key.xxx和Function.xxx不是同一类型。

例如:在Python中,我可以简单地执行以下操作:

keys = (
    ( Key.Orange, Function.Orange ),
    ( Key.Blue  , Function.Blue ),
    ( Key.Shift , Function.Shift ),
    ...
    )

psi_key = PsionTeklogix.Keyboard.Keyboard

for key, func in keys:
    if psi_key.GetModifierKeyState(key) == KeyState.Lock):
        psi_key.InjectKeyboardCommand(func, 0, 0)

在C#中做什么是“正确的方法”?

4 个答案:

答案 0 :(得分:13)

你可以做一些非常相似的事情:

Dictionary<Key, Function> keys = new Dictionary<Key, Function>
{
    { Key.Orange, Function.Orange },
    { Key.Blue, Function.Blue }
    ...
};

foreach (var pair in keys)
{
    if (Keyboard.GetModifierKeyState(pair.Key) == KeyState.Locked)
    {
        Keyboard.InjectKeyboardCommand(pair.Value, 0, 0);
    }
}

如果您愿意,甚至可以使用LINQ:

foreach (var pair in keys.Where(pair => 
                 Keyboard.GetModifierKeyState(pair.Key) == KeyState.Locked)
{
    Keyboard.InjectKeyboardCommand(pair.Value, 0, 0);
}

现在使用Dictionary有点奇怪,因为我们没有找到任何东西。如果您使用的是.NET 4,则可以使用元组列表:

var keys = new List<Tuple<Key, Function>>()
{
    Tuple.Of(Key.Orange, Function.Orange),
    Tuple.Of(Key.Blue, Function.Blue),
    ...
};

并相应地调整循环。您也可以使用匿名类型:

var keys = new[]
{
    new { Key = Key.Orange, Function = Function.Orange },
    new { Key = Key.Blue, Function = Function.Blue },
    ...
};

它们基本上都是表示键/函数对的方式:)

答案 1 :(得分:1)

假设Key和Function是枚举,您也可以尝试:

foreach (Key key in Enum.GetValues(typeof(Key))) 
{
    if (PsionTeklogix.Keyboard.Keyboard.GetModifierKeyState(key) == KeyState.Lock)
    {
        PsionTeklogix.Keyboard.Keyboard.InjectKeyboardCommand((Function)Enum.Parse(typeof(Function), key.ToString()), 0, 0);
    }
}

答案 2 :(得分:1)

using PsionTeklogix.Keyboard; /* assuming that's a namespace, */
                              /* otherwise, you can optionally do: */
/* using Keyboard = PsionTeklogix.Keyboard.Keyboard; */

class Foo{
    static readonly Key[] keys = {Key.Orange, Key.Blue, Key.Shift, ...};
    static readonly Function[] functions = {Function.Orange, Function.Blue, Function.Shift, ...};

    static void Main(){
        for(int i = 0; i < keys.Length; i++)
            if(Keyboard.GetModifierKeyState(keys[i]) == KeyState.Lock)
                Keyboard.InjectKeyboardCommand(func, 0, 0);
    }
}

或者,由于看起来KeyFunction都是枚举,,因此每个{{1}使用相同的Function枚举值名称}枚举值,你可以做这样的事情,这稍微漂亮,如果稍微慢一些:

Key

好吧,也许它不漂亮。 MEH。

但是,如果static readonly string[] values = {"Orange", "Blue", "Shift", ...}; static void Main(){ foreach(string value in values) if(Keyboard.GetModifierKeyState((Key)Enum.Parse(typeof(Key), value)) == KeyState.Lock); Keyboard.InjectKeyboardCommand((Function)Enum.Parse(typeof(Function), value), 0, 0); } 也与Key的值相同(即Function等),那么你可以这样做:

(int)Key.Orange == (int)Function.Orange

这些都不是Python代码的 direct 等价物,更像是这样:

static readonly Key[] keys = {Key.Orange, Key.Blue, Key.Shift, ...};

static void Main(){
    foreach(Key key in keys)
        if(Keyboard.GetModifierKeyState(key) == KeyState.Lock);
            Keyboard.InjectKeyboardCommand((Function)key, 0, 0);
}

这是所有人中最详细的解决方案,但它是最灵活的。

答案 3 :(得分:0)

您可以使用Dictionary

Dictionary<Key, Function> map = new Dictionary<Key, Function>();

map.Add(Key.Orange, Function.Orange);
map.Add(Key.Blue, Function.Blue);
map.Add(Key.Shift, Function.Shift);
map.Add(Key.Control, Function.Control);

foreach(var pair in map)
{
    if (PsionTeklogix.Keyboard.Keyboard.GetModifierKeyState(map.Key) == KeyState.Lock)
    {
        PsionTeklogix.Keyboard.Keyboard.InjectKeyboardCommand(map.Value, 0, 0);
    }
}