Skip to content

Inconsistency in the description of the called method in the documentation and in the source code (refers to the return type). #10811

Open
@Ika59

Description

@Ika59

Type of issue

Code doesn't work

Description

There is a contradiction in the description of the called method in the documentation and in the source code: in the source code, for called methods with one or two input parameters, there is always one return type - ValidationResult, while the documentation states that a method with one parameter must return the Boolean type.

Source code (from Git):

/// summary
/// Validation attribute that executes a user-supplied method at runtime, using one of these signatures:
/// public static Method(object value) { ... }
/// public static Method(object value, context) {.. }
/// '''
/// /summary
public sealed class CustomValidationAttribute : ValidationAttribute

[ . . . ]

// Invoke the method. Catch TargetInvocationException merely to unwrap it.
// Callers don't know Reflection is being used and will not typically see
// the real exception
try
{
// 1-parameter form is ValidationResult Method(object value)
// 2-parameter form is ValidationResult Method(object value, ValidationContext context),
var methodParams = _isSingleArgumentMethod
? new object?[] { convertedValue }
: new[] { convertedValue, validationContext };

   var result = (ValidationResult?)methodInfo!.Invoke(null, methodParams);

[ . . . ]

// Method must return a ValidationResult or derived class
if (!typeof(ValidationResult).IsAssignableFrom(methodInfo.ReturnType))
{
return SR.Format(SR.CustomValidationAttribute_Method_Must_Return_ValidationResult, Method, ValidatorType.Name);
}

[ . . . ]

// We accept 2 forms:
// 1-parameter form is ValidationResult Method(object value)
// 2-parameter form is ValidationResult Method(object value, ValidationContext context),

_isSingleArgumentMethod = (parameterInfos.Length == 1);

[ . . . ]

Page URL

https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.customvalidationattribute.method?view=net-9.0

Content source URL

https://github.com/dotnet/dotnet-api-docs/blob/main/xml/System.ComponentModel.DataAnnotations/CustomValidationAttribute.xml

Document Version Independent Id

ba1cc98b-e6b7-bf0a-9307-d450b7ff2a35

Article author

@dotnet-bot

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions