Table of Contents

Class UseComparerAttribute

Namespace
Nerdbank.Json
Assembly
Nerdbank.Json.dll

Selects the IEqualityComparer<T> or IComparer<T> that the deserializer uses when it constructs the collection for the decorated dictionary or set member.

[AttributeUsage(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
[AssociatedTypeAttribute("comparerType", TypeShapeRequirements.Constructor)]
[AssociatedTypeAttribute("comparerSource", TypeShapeRequirements.Constructor)]
public class UseComparerAttribute : Attribute
Inheritance
UseComparerAttribute
Inherited Members

Examples

Remarks

The serializer already exposes a global ComparerProvider, but that provider cannot preserve member-specific semantics such as a case-insensitive dictionary. Apply this attribute to a property, field, or constructor parameter whose type is a dictionary or set to control the comparer used for the freshly constructed collection during deserialization.

The comparer type must declare a public parameterless constructor and implement IEqualityComparer<T> or IComparer<T> for the collection's key or element type. It is activated through a source-generated type shape rather than runtime reflection, so the feature remains trimming-safe and NativeAOT-compatible. The comparer only influences the instances the deserializer creates; specify the same comparer in the member's initializer to influence instances that application code creates.

Getter-only collection initializers remain the preferred zero-configuration alternative: when a member is getter-only and already initialized with the desired comparer, the deserializer populates that instance and this attribute is unnecessary.

Constructors

UseComparerAttribute(Type)

Initializes a new instance of the UseComparerAttribute class.

public UseComparerAttribute(Type comparerType)

Parameters

comparerType Type

The comparer type to activate. It must declare a public parameterless constructor and implement IEqualityComparer<T> or IComparer<T> for the decorated collection's key or element type.

UseComparerAttribute(Type, string)

Initializes a new instance of the UseComparerAttribute class with a comparer provider.

public UseComparerAttribute(Type comparerSource, string memberName)

Parameters

comparerSource Type

The type that provides the comparer.

memberName string

The name of a public property that returns the comparer. Static properties require no provider instance; instance properties require a public parameterless constructor on the provider.

Properties

ComparerType

Gets the comparer type activated for the decorated collection member.

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor|DynamicallyAccessedMemberTypes.PublicProperties)]
public Type ComparerType { get; }

Property Value

Type

MemberName

Gets the name of the property on ComparerType that provides the comparer, or null when the type itself is the comparer.

public string? MemberName { get; }

Property Value

string