标记仅返回带有Conditional属性的Task的异步方法? [英] Mark async methods that just return a Task with the Conditional attribute?

查看:38
本文介绍了标记仅返回带有Conditional属性的Task的异步方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.Diagnostics.ConditionalAttribute对于无需使用编译器指令就可以分割Debug代码段非常有用,但是它仅与返回void的方法兼容.
有没有办法将它(或等效的东西)用于返回Task的异步方法?

The System.Diagnostics.ConditionalAttribute is really useful for sectioning off pieces of Debug code without needing to use compiler directives but it is only compatible with methods that return void.
Is there a way to use it (or something equivalent) for async methods that return a Task?

推荐答案

否.

可以无问题地删除void方法,因为无论是否有方法调用,对评估堆栈的最终影响都是相同的:零.

A void method can be removed without any problem because the net effect on the evaluation stack is the same whether there is a method call there or not: zero.

非空方法无法删除,因为在删除非空方法的情况下,堆栈上有零个东西,而在不删除非空方法的情况下,堆栈上有一个东西:对a的引用任务.

A non-void method cannot be removed because in the case where it is removed, there are zero things on the stack, and in the case where it is not removed, there is one thing on the stack: a reference to a task.

采用另一种方式:您希望这样做吗?

Put another way: what would you expect this to do:

Foo(BarAsync());

如果Foo使用Task并且BarAsync已删除?

这篇关于标记仅返回带有Conditional属性的Task的异步方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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