람다 식을 사용하는 Execute (..) 메서드의 우아한 구현을 찾으려고합니다. 내가 할 수있는 일이 가능한거야? 컴파일러가 (액션의 형태로) 그러한 람다 표현식을 전달할 수 있기 때문에 내가 할 수 있어야하는 것처럼 보입니다.비동기 주석을 기반으로 C# 람다 식을 실행하십시오.
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());
}
위의 실행 방법을 어떻게 구현합니까?
덕분에 내가 AsyncStateMachineAttribute를 사용하여 질문에 설명 무엇 달성 할 방법을 알고. 이 특별한 경우 나는 별도의 과부하를 피하려고 노력하고있다 ... 그것은 최후의 경우이다. –
@CraigSmitham :'Delegate.Method' 속성을 사용하여'MethodInfo'를 얻은 다음'method.IsDefined (typeof (AsyncStateMachineAttribute), false)'를 사용하여 그 존재를 검사 할 수 있습니다. 그것은 절대 안전한 것은 아닙니다. 누군가 속성 자체를 적용하는 것을 막을 수있는 방법은 없습니다. –
일단 내가 그것을 확인하면 그것을 실행하기 위해 어떻게 Func으로 취급하겠습니까? 나는 void 비동기 람다에서 Func 을 추출 할 수 없다. –