5
我正在试图找到一个优雅的执行(..)方法,该方法需要一个lambda表达式。我试图做甚至可能吗?看起来我应该能够,因为编译器将允许我传递这样一个lambda表达式(以Action的形式)。基于异步注释执行C#Lambda表达式
static void Main(string[] args)
{
// This should execute SomeOperation() synchronously
Execute(() => SomeOperation());
// This should begin execution of SomeOperationAsync(), but not wait (fire and forget)
Execute(() => SomeOperationAsync());
// This should await the execution of SomeOperationAsync() (execute synchronously)
Execute(async() => await SomeOperationAsync());
}
您将如何实现上述给定这些规范的Execute方法?
感谢乔恩,我很感激我知道如何实现问题中使用AsyncStateMachineAttribute概述。在这种特殊情况下,我试图避免单独的过载......这是一个边缘案例。 –
@CraigSmitham:那么你可以使用'Delegate.Method'属性来获得'MethodInfo',然后使用'method.IsDefined(typeof(AsyncStateMachineAttribute),false)'来检查它的存在。但这并非万无一失 - 没有任何东西可以阻止某个人自己使用属性。 –
一旦我检查它,我将如何将它作为Func来执行它?我似乎无法从无效的异步lambda中提取Func 。 –