Skip to content

Throw for invalid TryParse and BindAsync methods #36523

Closed
@BrennanConroy

Description

@BrennanConroy

If your TryParse method has a nullable out parameter and is a struct it fails to find the TryParse method and will throw:
System.InvalidOperationException : No public static bool MyBindAsyncRecord.TryParse(string, out MyBindAsyncRecord) method found for myBindAsyncRecord.

private record struct MyBindAsyncRecord(Uri Uri)
{
    public static bool TryParse(string? value, out MyBindAsyncRecord? result) =>
        throw new NotImplementedException();
}

This is because the method parameter type is Nullable`1[[MyBindAsyncRecord]]& and we pass MyBindAsyncRecord&

methodInfo = type.GetMethod("TryParse", BindingFlags.Public | BindingFlags.Static, new[] { typeof(string), typeof(IFormatProvider), type.MakeByRefType() });

Metadata

Metadata

Assignees

Labels

area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-minimal-actionsController-like actions for endpoint routingold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions