简体   繁体   English

如何将扩展方法与此和params关键字结合?

[英]How to combine extension method's this with params keyword?

I'd like to write an extension method: 我想写一个扩展方法:

static public bool IsKeyPressedAny(this params System.Windows.Input.Key[] keys)
{
    foreach (var key in keys)
    {
        if (System.Windows.Input.Keyboard.IsKeyDown(k))
        {
            return true;
        }
    }
    return false;
}

to be used like this 这样使用

IsKeyPressedAny(Key.LeftShift, Key.RightShift);

or 要么

IsKeyPressedAny(Key.a)

But the method's signature is invalid, adding this before params causes error CS1104: "A parameter array cannot be used with 'this' modifier on an extension method". 但是该方法的签名无效,请在参数导致错误CS1104之前添加签名:“参数数组不能与扩展方法的'this'修饰符一起使用”。

My current workaround is 我目前的解决方法是

static public bool IsKeyPressedAny(
    this System.Windows.Input.Key key
    , params System.Windows.Input.Key[] keys
)
{
    if (System.Windows.Input.Keyboard.IsKeyDown(key)) return true;

    foreach (var k in keys)
    {
        if (System.Windows.Input.Keyboard.IsKeyDown(k))
        {
            return true;
        }
    }
    return false;
}

which strikes me as a bit clunky. 这让我有些笨拙。 Is there a way to keep the benefits of using params while avoiding duplication of the argument type in the signature? 有没有一种方法可以保留使用参数的好处,同时又避免了签名中参数类型的重复?

(kind of a) Solution (一种)解决方案

The comments made me realize I was missusing this . 这些评论使我意识到我在滥用这一点 Since this is added as a method to the type it preceeds in the signature, using it before params is nonsense. 由于是作为方法添加到其在签名中使用的类型的,因此在params之前使用它是没有意义的。 I was trying to avoid typing the name of the class containing the method, this is not the way to solve that. 我试图避免键入包含该方法的类的名称, 但这 不是解决该问题的方法。

Consider to give the class a useful, somewhat specific name to get a nice syntax like this: 考虑为该类提供一个有用的,有点特定的名称,以得到如下所示的漂亮语法:

KeyPressed.Any(Key.LeftShift, Key.RightShift);

Is implemented by 由实施

public static class KeyPressed
{
    public static bool Any(params System.Windows.Input.Key[] keys)
    {
        ....
    }
}

Well, this doesn't really answer your question, but could still be a useful solution. 好吧,这并不能真正回答您的问题,但仍然可能是有用的解决方案。

this adds the method to the type it preceeds in the signature, using it before params makes no sense. 会将方法添加到其在签名中使用的类型,在没有意义的参数之前使用它。

this is not intended to save you typing the class' name containing the method. 并不是要节省您键入包含该方法的类的名称。

What you are looking for is called "using static", it imports things just like a regular using directive for namespaces: 您正在寻找的被称为“使用静态”,它像命名空间的常规using指令一样导入内容:

using static YourNameSpace.KeyHelpers;

will allow you to call YourNameSpace.KeyHelpers.IsKeyPressedAny by just it's name, without the class name: 将允许您仅通过名称调用YourNameSpace.KeyHelpers.IsKeyPressedAny ,而无需使用类名:

IsKeyPressedAny(Key.LeftShift, Key.RightShift);

Example: 例:

public static class KeyHelpers
{
    public static bool IsKeyPressedAny(params System.Windows.Input.Key[] keys)
    {
        foreach (var key in keys)
        {
            if (System.Windows.Input.Keyboard.IsKeyDown(k))
            {
                return true;
            }
        }
        return false;
    }
}

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

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