Skip to content

[API Proposal]: Provide trim-safe overload for ValidationContext construction #113134

Closed
@captainsafia

Description

@captainsafia

Background and motivation

The only trim-unsafe codepaths in the ValidationContext currently are invoked when no explicit Display is set on a ValidationContext. In this case, the ValidationContext will use unbounded reflection to discover the Display attribute on a type to support resolving it.

This presents a challenge to components that always initialize the DisplayName property as they always have to mark the construction of the ValidationContext as trim-safe even when it is not.

API Proposal

namespace System.ComponentModel.DataAnnotations;

public sealed class ValidationContext
{
    public ValidationContext(object instance, string displayName, IServiceProvider? serviceProvider = null, IDictionary<object, object?>? items = null)
}

API Usage

// Trim-safe initializtion of ValidationContext
var validationContext = new ValidationContext(42, "MyMagicNumber", null, null);

Alternative Designs

The proposed API only adds the displayName parameter to the most-specific constructor on the ValidationContext type. We can consider adding constructors with the other permutations of this type.

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedAPI was approved in API review, it can be implementedarea-System.ComponentModel.DataAnnotationsin-prThere is an active PR which will close this issue when it is merged

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions