StronglyTypedIdAttribute Class

Indicates that the type should be treated as a strongly-typed identifier and generates code to wrap a primitive value type. The source generator creates methods for conversion, comparison, serialization, and other common operations.

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false)]
public sealed class StronglyTypedIdAttribute : Attribute

Example

[StronglyTypedId(typeof(int))]
public partial struct UserId
{
}

Usage:

var userId = UserId.FromInt32(42);
var json = System.Text.Json.JsonSerializer.Serialize(userId);
Assert.Equal("42", json);
Inheritance
Attribute StronglyTypedIdAttribute

Constructors

NameDescription
StronglyTypedIdAttribute(Type, bool, bool, bool, bool, bool)Initializes a new instance of the StronglyTypedIdAttribute class.

StronglyTypedIdAttribute(Type, bool, bool, bool, bool, bool)

StronglyTypedIdAttribute.cs

Initializes a new instance of the StronglyTypedIdAttribute class.

public StronglyTypedIdAttribute(Type idType, bool generateSystemTextJsonConverter = true, bool generateNewtonsoftJsonConverter = true, bool generateSystemComponentModelTypeConverter = true, bool generateMongoDBBsonSerialization = true, bool addCodeGeneratedAttribute = true)

Parameters

idType Type
The underlying type of the strongly-typed identifier (e.g., Int32, Guid, String).
generateSystemTextJsonConverter bool
Specifies whether a System.Text.Json.Serialization.JsonConverter should be generated for JSON serialization.
generateNewtonsoftJsonConverter bool
Specifies whether a Newtonsoft.Json.JsonConverter should be generated for JSON serialization.
generateSystemComponentModelTypeConverter bool
Specifies whether a System.ComponentModel.TypeConverter should be generated for type conversion.
generateMongoDBBsonSerialization bool
Specifies whether a MongoDB.Bson.Serialization.Serializers.SerializerBase{T} should be generated for MongoDB BSON serialization.
addCodeGeneratedAttribute bool
Specifies whether to add GeneratedCodeAttribute to the generated members.

Properties

NameDescription
AddCodeGeneratedAttributeGets a value indicating whether generated members are marked with GeneratedCodeAttribute.
GenerateMongoDBBsonSerializationGets a value indicating whether a MongoDB BSON serializer is generated.
GenerateNewtonsoftJsonConverterGets a value indicating whether a Newtonsoft.Json converter is generated.
GenerateSystemComponentModelTypeConverterGets a value indicating whether a System.ComponentModel.TypeConverter is generated.
GenerateSystemTextJsonConverterGets a value indicating whether a System.Text.Json converter is generated.
GenerateToStringAsRecordGets or sets a value indicating whether the ToString() method should generate output in record format.
GuidGenerationStrategyGets or sets the strategy used by the generated New() method when the underlying type is Guid. Use GuidGenerationStrategy.None to not generate the method. When the property is not set, GuidGenerationStrategy.Version4 is used.
IdTypeGets the underlying type of the strongly-typed identifier.
StringComparisonGets or sets the string comparison method to use when the underlying type is String.

AddCodeGeneratedAttribute

StronglyTypedIdAttribute.cs

Gets a value indicating whether generated members are marked with GeneratedCodeAttribute.

public bool AddCodeGeneratedAttribute { get; }

Property value

bool

GenerateMongoDBBsonSerialization

StronglyTypedIdAttribute.cs

Gets a value indicating whether a MongoDB BSON serializer is generated.

public bool GenerateMongoDBBsonSerialization { get; }

Property value

bool

GenerateNewtonsoftJsonConverter

StronglyTypedIdAttribute.cs

Gets a value indicating whether a Newtonsoft.Json converter is generated.

public bool GenerateNewtonsoftJsonConverter { get; }

Property value

bool

GenerateSystemComponentModelTypeConverter

StronglyTypedIdAttribute.cs

Gets a value indicating whether a System.ComponentModel.TypeConverter is generated.

public bool GenerateSystemComponentModelTypeConverter { get; }

Property value

bool

GenerateSystemTextJsonConverter

StronglyTypedIdAttribute.cs

Gets a value indicating whether a System.Text.Json converter is generated.

public bool GenerateSystemTextJsonConverter { get; }

Property value

bool

GenerateToStringAsRecord

StronglyTypedIdAttribute.cs

Gets or sets a value indicating whether the ToString() method should generate output in record format.

public bool GenerateToStringAsRecord { get; set; }

Property value

bool

GuidGenerationStrategy

StronglyTypedIdAttribute.cs

Gets or sets the strategy used by the generated New() method when the underlying type is Guid. Use GuidGenerationStrategy.None to not generate the method. When the property is not set, GuidGenerationStrategy.Version4 is used.

public GuidGenerationStrategy GuidGenerationStrategy { get; set; }

Gets the underlying type of the strongly-typed identifier.

public Type IdType { get; }

Property value

Type

Gets or sets the string comparison method to use when the underlying type is String.

public StringComparison StringComparison { get; set; }

Property value

StringComparison