判斷Action Func 是否是異步方法

// 根據委託指向的方法時候包含async標記 Method.IsDefined(async)
//通用寫法
private static bool IsAsyncAppliedToDelegate(Delegate d)
{
    return d.Method.GetCustomAttribute(typeof(AsyncStateMachineAttribute)) != null;
}


//例子: 判斷 Action 和Func<TResult>
static bool IsThisAsync(Action action)
{
    return action.Method.IsDefined(typeof(AsyncStateMachineAttribute),false);
}
static bool IsThisAsync(Func<Task> action)
{
    return action.Method.IsDefined(typeof(AsyncStateMachineAttribute),false);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章