C# 迭代 switch 的情况

发布于 2024-08-03 10:32:44 字数 194 浏览 8 评论 0原文

是否可以以编程方式检索开关的所有大小写?我不知道,也许是 IL,但不知道该怎么做......

事实上,我的全局问题如下:我有一个 siwtch 案例,其中字符串作为属性名称。方法很重要,不允许回归。我不希望重构破坏这一点,所以我想要一种方法来测试所有大小写字符串实际上都是我的对象的真实属性。 (注意:默认值返回一些东西,所以我不能为重构的无效值抛出异常)。

Is it possible to retrieve programmatically all the case of a switch ? I don't have any idea, maybe by IL but not sure how to do ...

In fact my global issue is the following : I got a siwtch case with string as property name. The method is very important and a regression is not allowed. I don't want a refactoring breaking this, so I want a method to test that all case string are in fact real properties of my objects. (NB : the default value return something so I can't throw an exceptino for a refactored invalid value).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

荆棘i 2024-08-10 10:32:44

有多种方法可以避免在代码中直接使用属性名称作为字符串。我在此处发布了代码片段。

然后,不要使用 switch 语句,而是实现如下所示的内容:

private IDictionary<string, Action> _actions;

public void RegisterAction(string propertyName, Action action)
{
    _actions.Add(propertyName, action);
}

public void DoSomething(string propertyName)
{
    _actions[propertyName]();
}

当您调用这两个方法时,请确保使用代码片段的 Member 类(请参阅链接),而不是直接使用属性名称。因此,您可以确保您的代码是防重构的,因为它不包含任何“魔术字符串”。

此致

There are ways to avoid using property names as string directly in your code. I've posted a code snippet here.

Then, instead of using a switch statement, implement something like this:

private IDictionary<string, Action> _actions;

public void RegisterAction(string propertyName, Action action)
{
    _actions.Add(propertyName, action);
}

public void DoSomething(string propertyName)
{
    _actions[propertyName]();
}

When you call both methods, be sure that you use the Member class of the code snippet (see link) instead of using the property names directly. So you can make sure, that your code is refactoring-proof, because it does not contain any "magic strings".

Best Regards

耳根太软 2024-08-10 10:32:44

在 IL 中,switch 语句编译为如下内容:

// ...
L_000c: ldloc.1 
L_000d: ldstr "case1"
L_0012: call bool [mscorlib]System.String::op_Equality(string, string)
L_0017: brtrue.s L_0035
L_0019: ldloc.1 
L_001a: ldstr "case2"
L_001f: call bool [mscorlib]System.String::op_Equality(string, string)
L_0024: brtrue.s L_0042
L_0026: ldloc.1 
L_0027: ldstr "case3"
L_002c: call bool [mscorlib]System.String::op_Equality(string, string)
L_0031: brtrue.s L_004f
// ...

编写能够在所有情况下正确分析此语句的代码将需要大量工作。

我认为测试这一点的唯一方法是实际覆盖所有情况并确保您不会得到 null PropertyInfo。如果您正在使用的代码非常重要并且不能失败,那么在任何情况下使用反射都可能非常危险。你能转向更类型安全的设计吗?

In IL, a switch statement compiles to something like this:

// ...
L_000c: ldloc.1 
L_000d: ldstr "case1"
L_0012: call bool [mscorlib]System.String::op_Equality(string, string)
L_0017: brtrue.s L_0035
L_0019: ldloc.1 
L_001a: ldstr "case2"
L_001f: call bool [mscorlib]System.String::op_Equality(string, string)
L_0024: brtrue.s L_0042
L_0026: ldloc.1 
L_0027: ldstr "case3"
L_002c: call bool [mscorlib]System.String::op_Equality(string, string)
L_0031: brtrue.s L_004f
// ...

It would be a fair amount of work to write code that could analyze this properly in all cases.

The only way I can see to test this is to actually cover all cases and ensure you don't end up with a null PropertyInfo. If the code you are working with is very important and can't fail, it's probably pretty dangerous to use reflection in any case. Can you move to a more type-safe design?

清君侧 2024-08-10 10:32:44

获取开关中的值并非易事...

如果您在开关中使用枚举而不是字符串,则可以使用 Enum.GetValues 轻松获取枚举的值。

Getting the values in a switch would not be trivial...

If you use an enum in the switch instead of strings, you can easily get the values of the enum using Enum.GetValues.

夜深人未静 2024-08-10 10:32:44

用 AWK 或 Perl 或其他语言编写脚本来检查源本身,并在每次构建之前运行它。

Write a script in AWK or Perl or whatever to check the source itself, and run it before every build.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文