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> : AttributeType parameters
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
AttributeStronglyTypedIdAttribute<T>
Constructors
| Name | Description |
|---|---|
| StronglyTypedIdAttribute(bool, bool, bool, bool, bool) | Initializes a new instance of the StronglyTypedIdAttribute<T> class. |
StronglyTypedIdAttribute(bool, bool, bool, bool, bool)
StronglyTypedIdAttribute.csAvailable 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
generateSystemTextJsonConverterbool- Specifies whether a System.Text.Json.Serialization.JsonConverter should be generated for JSON serialization.
generateNewtonsoftJsonConverterbool- Specifies whether a Newtonsoft.Json.JsonConverter should be generated for JSON serialization.
generateSystemComponentModelTypeConverterbool- Specifies whether a System.ComponentModel.TypeConverter should be generated for type conversion.
generateMongoDBBsonSerializationbool- Specifies whether a MongoDB.Bson.Serialization.Serializers.SerializerBase{T} should be generated for MongoDB BSON serialization.
addCodeGeneratedAttributebool- Specifies whether to add
GeneratedCodeAttributeto the generated members.
Properties
| Name | Description |
|---|---|
| AddCodeGeneratedAttribute | Gets a value indicating whether generated members are marked with GeneratedCodeAttribute. |
| GenerateMongoDBBsonSerialization | Gets a value indicating whether a MongoDB BSON serializer is generated. |
| GenerateNewtonsoftJsonConverter | Gets a value indicating whether a Newtonsoft.Json converter is generated. |
| GenerateSystemComponentModelTypeConverter | Gets a value indicating whether a System.ComponentModel.TypeConverter is generated. |
| GenerateSystemTextJsonConverter | Gets a value indicating whether a System.Text.Json converter is generated. |
| GenerateToStringAsRecord | Gets or sets a value indicating whether the ToString() method should generate output in record format. |
| GuidGenerationStrategy | 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. |
| IdType | Gets the underlying type of the strongly-typed identifier. |
| StringComparison | Gets or sets the string comparison method to use when the underlying type is String. |
AddCodeGeneratedAttribute
StronglyTypedIdAttribute.csAvailable in: net10.0net11.0(not available in the other target frameworks of the package)
GenerateMongoDBBsonSerialization
StronglyTypedIdAttribute.csAvailable in: net10.0net11.0(not available in the other target frameworks of the package)
GenerateNewtonsoftJsonConverter
StronglyTypedIdAttribute.csAvailable in: net10.0net11.0(not available in the other target frameworks of the package)
GenerateSystemComponentModelTypeConverter
StronglyTypedIdAttribute.csAvailable in: net10.0net11.0(not available in the other target frameworks of the package)
GenerateSystemTextJsonConverter
StronglyTypedIdAttribute.csAvailable in: net10.0net11.0(not available in the other target frameworks of the package)
GenerateToStringAsRecord
StronglyTypedIdAttribute.csAvailable in: net10.0net11.0(not available in the other target frameworks of the package)
GuidGenerationStrategy
StronglyTypedIdAttribute.csAvailable 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; }Property value
Available in: net10.0net11.0(not available in the other target frameworks of the package)
StringComparison
StronglyTypedIdAttribute.csAvailable 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; }