dotnet / dotnet/dotnet-api-docs

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

Open
#10,811 2 comments 0 reactions 0 assignees View on GitHub
area-System.ComponentModel.DataAnnotations untriaged
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

### 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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.