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 : AttributeExample
[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
AttributeStronglyTypedIdAttribute
Constructors
| Name | Description |
|---|---|
| StronglyTypedIdAttribute(Type, bool, bool, bool, bool, bool) | Initializes a new instance of the StronglyTypedIdAttribute class. |
StronglyTypedIdAttribute(Type, bool, bool, bool, bool, bool)
StronglyTypedIdAttribute.csInitializes 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
idTypeType- The underlying type of the strongly-typed identifier (e.g.,
Int32,Guid,String). 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.csGenerateMongoDBBsonSerialization
StronglyTypedIdAttribute.csGenerateNewtonsoftJsonConverter
StronglyTypedIdAttribute.csGenerateSystemComponentModelTypeConverter
StronglyTypedIdAttribute.csGenerateSystemTextJsonConverter
StronglyTypedIdAttribute.csGenerateToStringAsRecord
StronglyTypedIdAttribute.csGuidGenerationStrategy
StronglyTypedIdAttribute.csGets 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
StringComparison
StronglyTypedIdAttribute.csGets or sets the string comparison method to use when the underlying type is String.
public StringComparison StringComparison { get; set; }