在编译时将开关绑定到Enum? [英] Binding a switch to an Enum at compile time?

查看:72
本文介绍了在编译时将开关绑定到Enum?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将switch语句绑定到Enum,如果并非在Enum中的所有值都在交换机中处理,则会引发编译时

错误

声明?我意识到你可以使用默认:捕获未处理的情况,但是

当然这只是在运行时。


示例:

public enum MyEnum

{

一,二,三,四

}


....


MyEnum myVar;

开关(myVar)

{

case MyEnum.one:

休息;

案例MyEnum.two:

休息;

案例MyEnum.three:

休息;

//我希望这会引发编译错误,因为没有案例

for MyVar.four

}


谢谢,Adam

解决方案

Adam Blair写道:< blockquote class =post_quotes>是否可以将switch语句绑定到Enum,如果并非在Enum中的所有值都在switch
语句中处理,则会引发编译时
错误?我意识到你可以使用默认:捕获未处理的案例,但是当然这只是在运行时。

例如:
public enum MyEnum
{
一,二,三,四



MyEnum myVar;
开关(myVar)
{
案例MyEnum.one:
休息;
案例MyEnum.two:
休息;
案例MyEnum.three:
休息;
//我希望这会引发编译错误,因为没有情况


默认值:

System.Diagnostics.Debug.Assert(false," unhandled enum value");

break;

for MyVar.four
}

谢谢,Adam




编译器选项不存在(或者我必须不知道它)。

我建议在default子句中调用Debug.Assert(false)。通过这种方式

您会收到任何未处理的值的通知,也就是说,在您的应用程序的调试版本

中。发布版本将默默忽略

断言语句。您也可以在默认的

情况下抛出异常,但这也会发布在发布版本中,这可能不会很有吸引力。

BTW,我在线添加了一些代码。


干杯,


Benoit。


感谢您的回复。你的想法很有用,但不幸的是它仍然是
依赖于遍历所有代码路径(开发人员正在观看输出

窗口)。


Adam


" Benoit Vreuninckx"写道:

Adam Blair写道:

是否可以将switch语句绑定到Enum,以便引发编译时
错误如果不是Enum中的所有值都在开关
语句中处理?我意识到你可以使用默认:捕获未处理的案例,但是当然这只是在运行时。

例如:
public enum MyEnum
{
一,二,三,四



MyEnum myVar;
开关(myVar)
{
案例MyEnum.one:
休息;
案例MyEnum.two:
休息;
案例MyEnum.three:
休息;
//我希望这会引发编译错误,因为没有案例



默认值:
System.Diagnostics.Debug.Assert(false,unhandled enum value" );
休息;

for MyVar.four


谢谢,Adam



那个编译器选项不存在(或者我必须不知道它)。
我建议在default子句中调用Debug.Assert(false)。这样就可以通知您任何未处理的值,即应用程序的调试版本。发布版本将默默忽略
assert语句。您也可以在默认的
情况下抛出异常,但这也可能发布在发布版本中,这可能不太吸引人。
顺便说一下,我添加了一些内联代码。 />
干杯,

Benoit。



编译错误在编译时没有任何意义时间,myVar的
值没有填充。

你确实想要一个运行时错误,而Debug.Assert是一个很好的建议

那里。 Debug.Assert不需要开发人员查看输出

窗口。


您可以随时抛出异常,以获得更具侵入性的警报。


- Sahil Malik
http:// dotnetjunkies.com/weblog/sahilmalik


" Adam Blair" < Adam Bl***@discussions.microsoft.com >在消息中写道

新闻:4A ********************************** @ microsof t.com ...

是否可以将switch语句绑定到Enum,以便在$中处理Enum中的所有值时,引发
编译时错误b $ b切换声明?我意识到你可以使用默认:捕获未处理的案例,但是当然这只是在运行时。

例如:
public enum MyEnum
{
一,二,三,四
}


MyEnum myVar;
开关(myVar)
{
案例MyEnum.one:
休息;
案例MyEnum.two:
休息;
案例MyEnum.three:
休息;
/ /我希望这会引发编译错误,因为没有案例
对于MyVar.four


谢谢,Adam



Is it possible to bind a switch statement to an Enum such that a compile-time
error is raised if not all values within the Enum are handled in the switch
statement? I realise you can use default: to catch unhandled cases, but of
course this is only at run-time.

Example:
public enum MyEnum
{
one, two, three, four
}

....

MyEnum myVar;
switch (myVar)
{
case MyEnum.one:
break;
case MyEnum.two:
break;
case MyEnum.three:
break;
// I would like this to raise a compilation error as there is no case
for MyVar.four
}

Thanks, Adam

解决方案

Adam Blair wrote:

Is it possible to bind a switch statement to an Enum such that a compile-time
error is raised if not all values within the Enum are handled in the switch
statement? I realise you can use default: to catch unhandled cases, but of
course this is only at run-time.

Example:
public enum MyEnum
{
one, two, three, four
}

....

MyEnum myVar;
switch (myVar)
{
case MyEnum.one:
break;
case MyEnum.two:
break;
case MyEnum.three:
break;
// I would like this to raise a compilation error as there is no case
default:
System.Diagnostics.Debug.Assert(false, "unhandled enum value");
break;
for MyVar.four
}

Thanks, Adam



That compiler option does not exist (or I must be unaware of it).
I suggest calling Debug.Assert(false) in the default clause. This way
you are notified of any unhandled values, that is, in the debug version
of your application. The release version will silently ignore the
assert statement. You could also throw an exception in the default
case, but this would also happen in the release version, which might not
be very appealing.
BTW, I added some code inline.

Cheers,

Benoit.


Thanks for your response. Your idea is useful, but unfortunately it still
relies on traversing all code paths (and developers to be watching the Output
window).

Adam

"Benoit Vreuninckx" wrote:

Adam Blair wrote:

Is it possible to bind a switch statement to an Enum such that a compile-time
error is raised if not all values within the Enum are handled in the switch
statement? I realise you can use default: to catch unhandled cases, but of
course this is only at run-time.

Example:
public enum MyEnum
{
one, two, three, four
}

....

MyEnum myVar;
switch (myVar)
{
case MyEnum.one:
break;
case MyEnum.two:
break;
case MyEnum.three:
break;
// I would like this to raise a compilation error as there is no case



default:
System.Diagnostics.Debug.Assert(false, "unhandled enum value");
break;

for MyVar.four
}

Thanks, Adam



That compiler option does not exist (or I must be unaware of it).
I suggest calling Debug.Assert(false) in the default clause. This way
you are notified of any unhandled values, that is, in the debug version
of your application. The release version will silently ignore the
assert statement. You could also throw an exception in the default
case, but this would also happen in the release version, which might not
be very appealing.
BTW, I added some code inline.

Cheers,

Benoit.



A compilation error doesn''t make sense there since during compile time, the
value of myVar is not populated.
You do want a runtime error though, and Debug.Assert is a good suggestion
there. Debug.Assert will not require the developers to look at the output
window.

You could always throw an exception for a little bit more intrusive alert.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik

"Adam Blair" <Adam Bl***@discussions.microsoft.com> wrote in message
news:4A**********************************@microsof t.com...

Is it possible to bind a switch statement to an Enum such that a compile-time error is raised if not all values within the Enum are handled in the switch statement? I realise you can use default: to catch unhandled cases, but of
course this is only at run-time.

Example:
public enum MyEnum
{
one, two, three, four
}

...

MyEnum myVar;
switch (myVar)
{
case MyEnum.one:
break;
case MyEnum.two:
break;
case MyEnum.three:
break;
// I would like this to raise a compilation error as there is no case
for MyVar.four
}

Thanks, Adam



这篇关于在编译时将开关绑定到Enum?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆