Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I target attributes for a record class?

When defining a record class, how do I target the attributes to the parameter, field or property?

For instance, I would like to use JsonIgnore but this doesn't compile as it has an attribute usage restriction to the field or property:

record Person(string FirstName, string LastName, [JsonIgnore] int Age);
like image 228
Daniel A. White Avatar asked Sep 07 '20 13:09

Daniel A. White


People also ask

What does the C# record keyword do?

Beginning with C# 9, you use the record keyword to define a reference type that provides built-in functionality for encapsulating data. C# 10 allows the record class syntax as a synonym to clarify a reference type, and record struct to define a value type with similar functionality.

Can C# record have methods?

You can define a record struct to create a record that is a value type. To enforce value semantics, the compiler generates several methods for your record type (both for record class types and record struct types): An override of Object. Equals(Object).

What is a positional record in C#?

Records in C# 9.0 provide positional syntax to declare and initialize property values of an instance. The compiler makes the properties public init-only auto-implemented when the user employs positional syntax to declare and initialize properties.

How do you explicitly identify the target of an attribute?

But you can also explicitly identify, for example, whether an attribute is applied to a method, or to its parameter, or to its return value. To explicitly identify an attribute target, use the following syntax: C#. [target : attribute-list] The list of possible target values is shown in the following table.

How do I add a target to a positional record?

You can add a targetto any attribute you apply to the positional record's properties. The following example applies the System.Text.Json.Serialization.JsonPropertyNameAttributeto each property of the Personrecord. The property:target indicates that the attribute is applied to the compiler-generated property.

How to apply attributes to auto-property and its backing field?

As noted in Microsoft documentation: Attributes can be applied to the synthesized auto-property and its backing field by using property: or field: targets for attributes syntactically applied to the corresponding >record parameter.

What are attribute attributes?

Attributes add metadata to your program. Metadata is information about the types defined in a program. All .NET assemblies contain a specified set of metadata that describes the types and type members defined in the assembly. You can add custom attributes to specify any additional information that is required.


2 Answers

To target the various parts of the expanded class, use the appropriate attribute target. For instance:

// Target the property, use `property`
record Person(string FirstName, string LastName, [property: JsonIgnore] int Age);

// Target the backing field of the property, use `field`
record Person(string FirstName, string LastName, [field: JsonIgnore] int Age);

// Target the constructor parameter, use `param`
record Person(string FirstName, string LastName, [param: SomeParamAttribute] int Age);
like image 124
Daniel A. White Avatar answered Oct 18 '22 20:10

Daniel A. White


A real-life example:

This record

public record GetAccountHolderParameters([property: JsonProperty("accountHolderCode")] string Code, [property: JsonProperty("showDetails")] bool ShowDetails);

or this preferred syntax which is better to read

public record GetAccountHolderParameters(
    [property: JsonProperty("accountHolderCode")] string Code,
    [property: JsonProperty("showDetails")] bool ShowDetails
);

is equivalent to

public class GetAccountHolderParameters
{
    [JsonProperty(PropertyName = "accountHolderCode")]
    public string Code { get; set; }

    [JsonProperty(PropertyName = "showDetails")]
    public bool ShowDetails { get; set; }
}
like image 1
KUTlime Avatar answered Oct 18 '22 22:10

KUTlime