Skip to content

How to verify generic return type in BuildEligibility for async methods? #318

Answered by svick
joakimriedel asked this question in Q&A
Discussion options

You must be logged in to vote

You can use Is with ConversionKind.TypeDefinition for this:

builder.ReturnType().MustSatisfy(
    type => type.Is(typeof(Task<ServiceResponse>)) ||
        (type.Is(typeof(Task<>), ConversionKind.TypeDefinition) &&
        ((INamedType)type).TypeArguments.Single().Is(typeof(ServiceResponse<>), ConversionKind.TypeDefinition)),
    t => $"{t} must be Task<ServiceResponse> or Task<ServiceResponse<T>>");

(I also changed the code to use builder.ReturnType(), but that's not necessary.)

Another option is to use GetAsyncInfo(), which will work for any awaitable type (like ValueTask<T>):

builder.ReturnType().MustSatisfy(
    type =>
    {
        var asyncInfo = type.GetAsyncInfo();

        return 

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@joakimriedel
Comment options

Answer selected by joakimriedel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants