Skip to content

TryParse parameter binding breaks if IParsable is explicitly implemented #43546

@halter73

Description

@halter73

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Example class:

public class MyTryParsableParam : IParsable<MyTryParsableParam>
{
    static bool IParsable<MyTryParsableArg>.TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out AddsCustomParameterMetadata result)
    {
        result = new();
        return true;
    }

    static MyTryParsableParam IParsable<MyTryParsableParam>.Parse(string s, IFormatProvider? provider) => throw new NotSupportedException();
}

Expected Behavior

ParameterBindingMethodCache should find the implicitly defined TryParse method and use it in RequestDelegateFactory and MVC's ModelMetadata.

Steps To Reproduce

It throws an exception that no matching method is found (despite finding it when generating the exception message :D)

Exceptions (if any)

System.InvalidOperationException : TryParse method found on AddsCustomParameterMetadata with incorrect format. Must be a static method with format
    bool TryParse(string, IFormatProvider, out AddsCustomParameterMetadata)
    bool TryParse(string, out AddsCustomParameterMetadata)
but found
    static Boolean TryParse(System.String, System.IFormatProvider, AddsCustomParameterMetadata ByRef)

.NET Version

main branch

Anything else?

We could special-case IParsable because we have a reference to it. The check for !method.IsAbstract in GetStaticMethodFromHierarchy stops us from finding it now. But even if we did find it, we haven't implemented the correct code gen to call it for unreferenced interfaces. We might have to use Type.GetInterfaceMap. See previous discussion: #40926 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-minimal-actionsController-like actions for endpoint routingfeature-model-binding

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions