StronglyTypedIdAttribute<T> Class

Indicates that the type should be treated as a strongly-typed identifier with compile-time type safety. This generic variant provides type checking at compile time and generates code to wrap the specified value type.

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

Type parameters

T
The underlying type of the strongly-typed identifier (e.g., Int32, Guid, String).

Example

[StronglyTypedId<Guid>]
public partial struct ProductId
{
}

Usage:

var productId = ProductId.New();
var json = System.Text.Json.JsonSerializer.Serialize(productId);
Assert.Equal($"\"{productId.Value}\"", json);
Inheritance
Attribute StronglyTypedIdAttribute<T>

Constructors

NameDescription
StronglyTypedIdAttribute(bool, bool, bool, bool, bool)Initializes a new instance of the StronglyTypedIdAttribute<T> class.

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

StronglyTypedIdAttribute.cs

Available in: net10.0net11.0(not available in the other target frameworks of the package)

Initializes a new instance of the StronglyTypedIdAttribute<T> class.

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

Parameters

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

Available in: net10.0net11.0(not available in the other target frameworks of the package)

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

public bool AddCodeGeneratedAttribute { get; }

Property value

bool

GenerateMongoDBBsonSerialization

StronglyTypedIdAttribute.cs

Available in: net10.0net11.0(not available in the other target frameworks of the package)

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

public bool GenerateMongoDBBsonSerialization { get; }

Property value

bool

GenerateNewtonsoftJsonConverter

StronglyTypedIdAttribute.cs

Available in: net10.0net11.0(not available in the other target frameworks of the package)

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

public bool GenerateNewtonsoftJsonConverter { get; }

Property value

bool

GenerateSystemComponentModelTypeConverter

StronglyTypedIdAttribute.cs

Available in: net10.0net11.0(not available in the other target frameworks of the package)

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

public bool GenerateSystemComponentModelTypeConverter { get; }

Property value

bool

GenerateSystemTextJsonConverter

StronglyTypedIdAttribute.cs

Available in: net10.0net11.0(not available in the other target frameworks of the package)

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

public bool GenerateSystemTextJsonConverter { get; }

Property value

bool

GenerateToStringAsRecord

StronglyTypedIdAttribute.cs

Available in: net10.0net11.0(not available in the other target frameworks of the package)

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

Available in: net10.0net11.0(not available in the other target frameworks of the package)

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; }

Available in: net10.0net11.0(not available in the other target frameworks of the package)

Gets the underlying type of the strongly-typed identifier.

public Type IdType { get; }

Property value

Type

Available in: net10.0net11.0(not available in the other target frameworks of the package)

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

public StringComparison StringComparison { get; set; }

Property value

StringComparison