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".
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. I was trying to avoid typing the name of the class containing the method, this is not the way to solve that.