当使用自定义属性引入破坏代码时,我们可以强制出现编译时错误吗?
我要问的问题涉及 如何编写其逻辑免受未来附加枚举影响的代码?,但有可能更通用。
为了简单起见,我只会考虑这两个答案(1,2) 到 问题,因为我不想过多地重构代码。
解决方案 1 当 switch 值是未专门处理的枚举值时,通过在 switch 语句的默认块中抛出运行时异常来处理这种情况。如果在测试期间发生这种情况,系统将提示开发人员重新检查代码并在 switch 语句中添加针对该特定枚举值的处理代码。
解决方案 2 通过使用访问者模式来完成此操作,因此如果使用该枚举的代码未更新以反映附加值,则任何枚举值的添加都将创建编译时错误。
现在,这两种解决方案都是有效且良好的。但是,与运行时异常相比,我更喜欢编译时错误,以防测试期间的代码覆盖率跳过该部分。另外,编写像解决方案 1 这样的代码比解决方案 2 更自然(太长)。
因此我想知道是否有一个解决方案既具有解决方案2的编译时错误的优点又具有解决方案1中简单添加代码的优点?是否可以编写一些自定义属性来指定在开关中使用的特定枚举必须对照该开关中所有可能的枚举值进行检查,并让编译器在违反此规定时进行投诉。
[allValuesMustBeCheckedInSwitch]
enum Fruit {Apple, Banana, Coconut}
Fruit aFruit = Fruit.Apple;
switch (aFruit)
{
case Fruit.Apple:
//do something
break;
case Fruit.Banana:
//do something
break;
default:
break;
}
如果我可以在开关中添加类似的内容以选择性地禁用编译器错误(也许将其更改为警告),那就更好了。
[checkOnly(Fruit.Apple,Fruit.Banana)]
我说我的问题更笼统,因为我想知道自定义属性的表现力如何,以前没有使用过属性。此示例只是我想在代码中定义的一个特定限制,可能还有其他不同类型的限制。
如果还有其他工具可以执行类似的任务,我也想了解一下。
The question I'm asking pertains to How do you write code whose logic is protected against future additional enumerations?, but has the possibility to be more general.
For simplicity's sake, I will only consider these two answers (1, 2) to that question, because I do not want to have to restructure the code too much.
Solution 1 handles the situation by throwing a run-time exception in the default block of switch statement when the switch value is an enum value which is not handled specifically. If this happens during testing, the developer will be prompted to reexamine the code and add handling code for that particular enum value in the switch statement.
Solution 2 does it by using the visitor pattern, so any addition of enum values will create a compile-time if code which use that enum is not updated to reflect the additional value.
Now, both solutions are valid and good. However, I prefer compile-time error over run-time exception in case my code coverage during testing skips that part. Also, it's more natural to write code like solution 1 over solution 2 (too lengthy).
Hence I would like to know if there is a solution that has both the goodness of compile-time error of solution 2 and the simple addition of code in solution 1? Is it possible to write some custom attribute to specify that a particular enum when used in a switch must be checked against all possible enum values in that switch, and for the compiler to complain when this is violated.
[allValuesMustBeCheckedInSwitch]
enum Fruit {Apple, Banana, Coconut}
Fruit aFruit = Fruit.Apple;
switch (aFruit)
{
case Fruit.Apple:
//do something
break;
case Fruit.Banana:
//do something
break;
default:
break;
}
It will even be better if I can add something like this to the switch to selectively disable the compiler error (and perhaps change it to a warning).
[checkOnly(Fruit.Apple,Fruit.Banana)]
I said that my question is more general because I would like to know how expressive custom attributes can be, not having used attributes before. This example is just be one particular restriction I would like to define in my code, there could be other different types of restrictions.
If there are other tools which can perform similar tasks, I would like to know about it too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定 CodeContracts 是否可行,但请务必检查一下,也许它适合您的需求。
I'm not sure if it's possible with CodeContracts, but be sure to check that out, maybe it suits your needs.