- Notifications
You must be signed in to change notification settings - Fork0
Runtime check if an object or type can be awaited.
License
tommasobertoni/IsAwaitable
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
branch | build | coverage | quality |
---|---|---|---|
main | |||
dev |
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.
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 expression
t
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
// 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
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
Created byThe Icon Z fromThe Noun Project.
About
Runtime check if an object or type can be awaited.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.