令人信服的C#编译器执行将成员返回后停止 [英] convincing C# compiler that execution will stop after a member returns

查看:180
本文介绍了令人信服的C#编译器执行将成员返回后停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不认为这是目前可能的,或者如果它甚至是一个好主意,但它的东西,我想刚才。我使用MSTest的单元测试我的C#项目。在我的测试之一,我做以下内容:

I don't think this is currently possible or if it's even a good idea, but it's something I was thinking about just now. I use MSTest for unit testing my C# project. In one of my tests, I do the following:

MyClass instance;

try
{
    instance = getValue();
}
catch (MyException ex)
{
    Assert.Fail("Caught MyException");
}

instance.doStuff(); // Use of unassigned local variable 'instance'



为了使这个代码编译,我有一个分配值实例无论是在其声明中或在块。我也可以收益 Assert.Fail 但是这仍然是一个解决办法,而不是仅仅编译知道的不能执行此点之后继续。 Assert.Fail 永远不会,尽我所知,允许执行继续过去吧,因此实例永远没有值被使用。为什么那时,我必须分配一个值呢?如果我更改 Assert.Fail 来像抛出前,代码编译好,我想因为它知道除了将禁止执行进行到实例将用于初始化一个点。

To make this code compile, I have to assign a value to instance either at its declaration or in the catch block. I could alternatively return after the Assert.Fail but that's still a workaround instead of the compiler just knowing that execution cannot continue after this point. Assert.Fail will never, to the best of my knowledge, allow execution to proceed past it, hence instance will never be used without a value. Why is it then that I must assign a value to it? If I change the Assert.Fail to something like throw ex, the code compiles fine, I assume because it knows that exception will disallow execution to proceed to a point where instance would be used uninitialized.

反之,如果我没不想测试失败,而是被标记为不确定的?我可以做一个 Assert.Inconclusive 而不是失败的,如果编译器知道执行将不会继续这将是很好之后。

Contrariwise, what if I didn't want the test to fail, but rather be marked as inconclusive? I could do an Assert.Inconclusive instead of Fail, and it would be nice if the compiler knew execution would not continue after that.

因此,它是运行时对案件编译时间在哪里执行将被允许继续进行知识呢?这将永远是合理的C#有说,一个成员的某种方式,在这种情况下 Assert.Fail ,绝不允许执行返回后?也许这可能是一个方法属性的形式。这将是有益的或者编译器的不必要的复杂性?

So is it a case of runtime versus compile-time knowledge about where execution will be allowed to proceed? Would it ever be reasonable for C# to have some way of saying that a member, in this case Assert.Fail, will never allow execution after it returns? Maybe that could be in the form of a method attribute. Would this be useful or an unnecessary complexity for the compiler?

外单元测试

由于人[有效]指出,这是写单元测试,考虑单元测试的领域之外,我的问题一个愚蠢的方式:

Since people are [validly] pointing out that this is a silly way to write a unit test, consider my question outside the realm of unit testing:

MyClass instance;

if (badThings)
{
    someMethodThatWillNeverReturn();
}
else
{
    instance = new MyClass();
}

instance.doStuff();

下面可能我可以取代调用 someMethodThatWillNeverReturn 与抛出异常,也许如果我有事情要做,我能做到这一点在构造函数中的异常。

Here potentially I could replace the call to someMethodThatWillNeverReturn with throwing an exception, and perhaps if I had stuff to do, I could do it in the constructor for the exception.

ReSharper的谁知

如果我添加一个收益 Assert.Fail Assert.Inconclusive ,ReSharper的颜色收益灰色并有提示说的代码是试探性地无法访问。

If I add a return after Assert.Fail or Assert.Inconclusive, Resharper colors return gray and has a tooltip saying "Code is heuristically unreachable."

推荐答案

是的,这将是合理的,有一些这表明一个成员将永远不会完成正常 - 即断言,成员之后的一点是无法访问。 (这既可以是由于异常或因无休止的循环下去。)

Yes, it would be reasonable to have something which indicated that a member would never complete normally - i.e. asserting that the point after the member was unreachable. (This could either be due to an exception or due to looping forever.)

您会希望有要的的东西的(无论是在CLR或编译器),以拿出如果你错了一个备份计划:如果有人改变了 Assert.Fail 来正常返回,会发生什么?你可能想代码验证的一部分,是东西,检查它永远不会正常返回。

You'd want there to be something (whether in the CLR or the compiler) to come up with a backup plan for if you're wrong: what would happen if someone changed Assert.Fail to return normally? You'd potentially want part of code verification to be something that checked it would never return normally.

我相信有关于某人在微软这个想法的博客帖子..我会看看我能找到它。

I believe there's a blog post about this idea from someone in Microsoft... I'll see if I can find it.

在对代表它的语法方面,虽然属性是一个显而易见的想法,我很喜欢的一个回报的想法类型为从不。显然,这将有可能与现有的从不类型的冲突,但嘿...

In terms of syntax for representing it, while an attribute is an obvious idea, I quite like the idea of a return type of "never". Obviously that would potentially clash with existing "never" types, but hey...

在其实用性方面:明显的解决方法是将语句后立即抛出一个异常,但它肯定讨厌不得不这样做。 (这是一般比一个更好的回报,因为它意味着如果你正在写这在方法有一个返回类型,你不必指定一个毫无意义的返回值 - 你也不需要确保所有退出参数赋值),所以它不是的需要的 - 但我认为这将是很好。无论是C#团队可以用自己有限的预算做的最重要的事情就是另一回事了 - 只是为了抢先埃里克;)

In terms of its usefulness: the obvious workaround is to throw an exception immediately after the statement, but it's certainly annoying to have to do that. (It's generally better than a return as it means if the method you're writing this in has a return type, you don't have to specify a pointless return value - and you also don't need to make sure that all out parameters are assigned values.) So it's not required - but I think it would be nice. Whether it's the most important thing the C# team can do with their limited budget is a different matter - just to pre-empt Eric ;)

这篇关于令人信服的C#编译器执行将成员返回后停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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