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);
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.
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).
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.
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.
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.
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.
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.
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);
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; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With