[英]C# parse from string to Keys
Currently I have to parse from string to Keys somehow. 目前我必须以某种方式从字符串解析到Keys。 So basically as input I get something like "Keys.Shift" and somehow I need be able to parse this to keys so I can use it futher in the application.
所以基本上作为输入我得到类似“Keys.Shift”的东西,并且我需要能够将其解析为键,以便我可以在应用程序中进一步使用它。
I've found a solution but it doesn't work: 我找到了一个解决方案,但它不起作用:
Keys key;
Enum.TryParse("Enter", out key);
I get a "static types cannot be used as type arguments". 我得到一个“静态类型不能用作类型参数”。 Does someone know a workaround or something?
有人知道解决方法或其他什么吗?
Thanks in advance. 提前致谢。
It sounds like you've got another class called Keys
somewhere. 听起来你在某个地方有另一个名为
Keys
课程。 Here's an example demonstrating the same problem (although there's a second error around the declaration of key
which you haven't mentioned; I suspect you've got that error as well though): 这是一个展示同样问题的例子(虽然你没有提到的
key
声明有第二个错误;我怀疑你也有错误):
using System;
using System.Windows.Forms;
static class Keys {}
class Program
{
static void Main()
{
Keys key;
Enum.TryParse("Enter", out key);
Console.WriteLine(key);
}
}
If you comment out the static class Keys {}
the code compiles fine, which is why I suspect you've got that class somewhere - or a using
directive bringing something similar in from another library. 如果你注释掉
static class Keys {}
,代码编译得很好,这就是为什么我怀疑你在某个地方得到了那个类 - 或者是一个using
指令从另一个库中带来了类似的东西。
The simplest fix is just to fully-qualify which Keys
type you mean: 最简单的解决方法是完全限定您的意思是哪种
Keys
类型:
using System;
using System.Windows.Forms;
static class Keys {}
class Program
{
static void Main()
{
System.Windows.Forms.Keys key;
Enum.TryParse("Enter", out key);
Console.WriteLine(key);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.