CoreOne.Generators
1.0.0
See the version list below for details.
dotnet add package CoreOne.Generators --version 1.0.0
NuGet\Install-Package CoreOne.Generators -Version 1.0.0
<PackageReference Include="CoreOne.Generators" Version="1.0.0" />
<PackageVersion Include="CoreOne.Generators" Version="1.0.0" />
<PackageReference Include="CoreOne.Generators" />
paket add CoreOne.Generators --version 1.0.0
#r "nuget: CoreOne.Generators, 1.0.0"
#:package CoreOne.Generators@1.0.0
#addin nuget:?package=CoreOne.Generators&version=1.0.0
#tool nuget:?package=CoreOne.Generators&version=1.0.0
CoreOne.Generators
A Roslyn source generator that produces strongly-typed IDs from a single attribute. Eliminates boilerplate for value types that wrap a primitive (e.g. OrderId wrapping a Guid).
Usage
Decorate any partial type with [StronglyTypedId]:
// Generic form (C# 11 / .NET 7+)
[StronglyTypedId<Guid>]
public partial struct OrderId { }
// Non-generic form (works everywhere)
[StronglyTypedId(typeof(int))]
public partial struct UserId { }
The generator emits a .g.cs file alongside your type with all members filled in automatically.
What Gets Generated
Core Members
| Member | Notes |
|---|---|
T Value |
The underlying value |
static T Empty |
Default/empty instance (Guid.Empty, "", etc.) |
string ValueAsString |
Culture-invariant string representation |
TypeName() |
Default constructor |
static TypeName From{T}(T value) |
Factory method |
static TypeName New() |
Guid types — wraps Guid.NewGuid() |
static TypeName Create() |
Guid types — wraps ID.Create() (sequential GUID) |
ToString(), GetHashCode(), Equals() |
Value-based equality |
==, != operators |
Value-based comparison |
TryParse, Parse |
String parsing |
IParsable<T>, ISpanParsable<T> |
.NET 7+ only |
IComparable, <, <=, >, >= |
When declared on the partial type |
All members are additive — any member you define yourself is skipped by the generator.
Serialization Converters
By default, the generator also emits nested converter classes and applies the corresponding attributes to your type:
| Converter | Generated Type |
|---|---|
System.Text.Json |
{TypeName}JsonConverter |
Newtonsoft.Json |
{TypeName}NewtonsoftJsonConverter |
System.ComponentModel.TypeConverter |
{TypeName}TypeConverter |
MongoDB.Bson |
{TypeName}BsonConverter |
Each converter is only emitted when the relevant library is detected in the consuming project.
Supported Underlying Types
22 primitive types are supported:
bool · byte · sbyte · short · ushort · int · uint · long · ulong ·
Int128 · UInt128 · BigInteger · float · double · decimal · Half ·
Guid · string · DateTime · DateTimeOffset · MongoDB.Bson.ObjectId
Attribute Options
[StronglyTypedId<Guid>(
generateSystemTextJsonConverter: true, // default
generateNewtonsoftJsonConverter: true, // default
generateSystemComponentModelTypeConverter: true, // default
generateMongoDBBsonSerialization: true, // default
addCodeGeneratedAttribute: true, // default
StringComparison = StringComparison.Ordinal, // string IDs only
GenerateToStringAsRecord = false
)]
public partial struct OrderId { }
Set any converter flag to false to suppress that converter entirely.
Sequential GUIDs
For Guid-backed IDs, Create() uses ID.Create() from the CoreOne library, which generates sequential (v7-style) GUIDs. This is friendlier to database indexes than random GUIDs.
var id = OrderId.Create(); // sequential GUID — good for DB inserts
var id = OrderId.New(); // random Guid.NewGuid()
Setup
Add the generator project as an analyzer reference — no assembly reference is needed at runtime:
<ProjectReference Include="..\CoreOne.Generators\CoreOne.Generators.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.14.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.