Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Runtime check if an object or type can be awaited.

License

NotificationsYou must be signed in to change notification settings

tommasobertoni/IsAwaitable

Repository files navigation

IsAwaitable

License MITNugetnetstandard2.0

branchbuildcoveragequality
mainCICoverageCodeFactor Grade
devCICoverageCodeFactor Grade

Given an infinite amount of time, everything that can happen will eventually happen... includingneeding to know at runtime if an object or type can be awaited.

TL;DR

The library provides the following extension methods:

usingSystem.Threading.Tasks;// Checks if it's awaitableboolIsAwaitable(thisobject?instance);boolIsAwaitable(thisTypetype);// await x;// Check if it's awaitable and returns a resultboolIsAwaitableWithResult(thisobject?instance);boolIsAwaitableWithResult(thisobject?instance,outType?resultType);boolIsAwaitableWithResult(thisTypetype);boolIsAwaitableWithResult(thisTypetype,outType?resultType);// var foo = await x;

...and some bonus ones:

usingIsAwaitable;// Known awaitables: Task, Task<T>, ValueTask, ValueTask<T>boolIsKnownAwaitable(thisobject?instance);boolIsKnownAwaitable(thisTypetype);// Is Task<T> or ValueTask<T>boolIsKnownAwaitableWithResult(thisobject?instance);boolIsKnownAwaitableWithResult(thisobject?instance,outType?resultType);boolIsKnownAwaitableWithResult(thisTypetype);boolIsKnownAwaitableWithResult(thisTypetype,outType?resultType);

If you want to seehow a type, or instance, is compliant with an awaitable expression, you can use theAwaitable type:

usingIsAwaitable.Analysis;_=Awaitable.Describe("hello");// nullvardescription=Awaitable.Describe(typeof(MyCustomAwaitableType));if(descriptionis notnull){varresultType=description.ResultType;}vartaskDescription=Awaitable.Describe<Task>();varisKnownAwaitable=taskDescripti.IsKnownAwaitable;

TheDescribe function inspects the type to check if it matches thec# language specification for awaitable expressions:

An expressiont is awaitable if one of the following holds:

  • t is of compile time typedynamic
  • t has an accessible instance or extension method calledGetAwaiter with no parameters and no type parameters, and a return typeA for which all of the following hold:
    • A implements the interfaceINotifyCompletion
    • A has an accessible, readable instance propertyIsCompleted of typebool
    • A has an accessible instance methodGetResult with no parameters and no type parameters

Usage

// On instancesTaskdoAsync=DoSomethingAsync();_=doAsync.IsAwaitable();// true// Returing a resultTask<int>promise=GetSomethingAsync();_=promise.IsAwaitable();// true_=promise.IsAwaitableWithResult();// true// On types_=typeof(Task).IsAwaitable();// true// On value tasks_=typeof(ValueTask).IsAwaitable();// true_=typeof(ValueTask<>).IsAwaitableWithResult();// true// On custom awaitables!classCustomDelay{privatereadonlyTimeSpan_delay;publicCustomDelay(TimeSpandelay)=>_delay=delay;publicTaskAwaiterGetAwaiter()=>Task.Delay(_delay).GetAwaiter();}vardelay=newCustomDelay(TimeSpan.FromSeconds(2));_=delay.IsAwaitable();// true_=delay.IsAwaitableWithResult();// false

Dynamically await anything

asyncTask<object>AwaitResultOrReturn(objectinstance){returninstance.IsAwaitableWithResult()?await(dynamic)instance:instance;}varfoo=GetFoo();varfooTask=Task.FromResult(foo);varresult1=awaitAwaitResultOrReturn(foo);varresult2=awaitAwaitResultOrReturn(fooTask);// foo == result1 == result2

Continuous Integration

github-actionsxUnitcoverletcoveralls.iocodefactor.io

Icon

Created byThe Icon Z fromThe Noun Project.

About

Runtime check if an object or type can be awaited.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

[8]ページ先頭

©2009-2025 Movatter.jp