switch 语句:“期望一个常量值”
目前,我正在与“神奇字符串”问题作斗争:
public class MyDataField
{
// class definition
}
// exuecuted method
public void SwitchMultipleDataFields()
{
var myField = new MyDataField();
switch(myField.GetType().ToString())
{
// only case, which works
case "MyDataField":
// case operations
break;
// other option:
case typeof(MyDataField).ToString():
// case operations
break;
// other cases of other FieldTypes
}
}
现在我收到了我在线程标题中编写的错误消息。我认为问题在于这个字符串在“非编译时”时不是常量。因此,询问 switch 的唯一可能方法是通过显式确定该 case 字符串的值。我的问题是,如果我重命名 MyDataField
类,我不会收到编译错误。所以无论如何,这些类中的 90% 都是通用的。这些在 switch 语句的 default
中处理。除了明确确定 case 值之外,是否还有其他方法?
请不要争论这个方法的意义。我刚刚写这个是为了以更简单的方式说明我的问题
Currently I'm fighting with that "magical strings" issue:
public class MyDataField
{
// class definition
}
// exuecuted method
public void SwitchMultipleDataFields()
{
var myField = new MyDataField();
switch(myField.GetType().ToString())
{
// only case, which works
case "MyDataField":
// case operations
break;
// other option:
case typeof(MyDataField).ToString():
// case operations
break;
// other cases of other FieldTypes
}
}
Now I get the error Message I've written in the title of my thread. I think the problem is that this string is not a constant while "non-compile-time". So the only possible way to ask switch this is via explicitly determining the value of that case string. My problem just is that I don't get an compile error in case I'd rename the MyDataField
class. So 90% of these classes are generic anyway. These are handled in the default
of the switch statement. Isn't there another way than explicitly determining the value of the case value?
Please don't argue about the sense of this method. I've just written that to illustrate my problem in an easier way
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需使用
if
:您甚至不需要比较类型名称,而是直接比较
Type
对象(引用)。Just use an
if
:You even don't need to compare type names, but the
Type
objects (references) directly.我建议您参考规范§8.7.2,其中规定了
switch-label
的语法:简单地说,case 标签在编译时必须是常量。请注意,
typeof(MyDataField).ToString()
不是编译时常量(它对您来说可能看起来是常量,但这并不是因为它无法在编译时完全求值)。规范的第 7.19 节非常清楚地说明了常量是什么。您需要将其重新编码为
if/else if/else
。I refer you to the specification §8.7.2 which states for the grammar of a
switch-label
:Simply put, the case labels must be constants at compile-time. Note that
typeof(MyDataField).ToString()
is not a compile-time constant (it might look constant to you, but it's not because it can not be fully evaluated at compile time). §7.19 of the specification spells out very clearly what a constant isYou need to recode this as an
if/else if/else
.case 语句需要一个常量值,因此您
需要将其更改为您正在查找的特定字符串:
如果您尝试确定字段类型,您可以执行以下操作:
the case statement requires a constant value, so where you have
you would need to change that to the specific string that you are looking for:
if you are trying to determine the field type, you can do something like this:
使用新的模式匹配功能 的 C# 7 我将通过以下方式解决它。
这里是一个简单的
Field
和Document
类,这里是一个
FieldOperator
类,它对Field
列表进行一些任意更改测试这些“操作”的正确性
With the new pattern matching feature of C# 7 I would solve it in the following manner.
Here a simple
Field
andDocument
classAnd here a
FieldOperator
class which does some arbitrary changes to a list ofField
sTesting for correctness of these "operations"
使用函数重载,也许还可以使用
dynamic
关键字。或者使用访客模式。无论哪种方式,您都可以根据变量的运行时类型进行调度。Use function overloading, and maybe the
dynamic
keyword. Or use the Visitor pattern. Either way gives you dispatch based on the runtime type of a variable.