Bcss.ToStringGenerator.Attributes
1.0.0
dotnet add package Bcss.ToStringGenerator.Attributes --version 1.0.0
NuGet\Install-Package Bcss.ToStringGenerator.Attributes -Version 1.0.0
<PackageReference Include="Bcss.ToStringGenerator.Attributes" Version="1.0.0" />
<PackageVersion Include="Bcss.ToStringGenerator.Attributes" Version="1.0.0" />
<PackageReference Include="Bcss.ToStringGenerator.Attributes" />
paket add Bcss.ToStringGenerator.Attributes --version 1.0.0
#r "nuget: Bcss.ToStringGenerator.Attributes, 1.0.0"
#:package Bcss.ToStringGenerator.Attributes@1.0.0
#addin nuget:?package=Bcss.ToStringGenerator.Attributes&version=1.0.0
#tool nuget:?package=Bcss.ToStringGenerator.Attributes&version=1.0.0
ToStringGenerator
A C# source generator that automatically creates customizable ToString()
implementations for your classes, with built-in support for sensitive data handling.
Features
- 🚀 Automatic
ToString()
generation using source generators - 🔒 Built-in support for masking sensitive data
- 📦 Works with any type including collections, dictionaries, and nullable types
- 🎯 Zero runtime overhead—all code is generated at compile time
- ✨ Clean and readable output format
Installation
Add the NuGet package to your project:
dotnet add package Bcss.ToStringGenerator
Usage
Basic Usage
- Add the
[GenerateToString]
attribute to your class - Make your class
partial
- The source generator will automatically create a
ToString()
implementation at compile-time.
using Bcss.ToStringGenerator.Attributes;
[GenerateToString]
public partial class User
{
public string Username { get; set; }
public List<string> Addresses { get; set; } = [];
public Dictionary<string, string> Preferences {get; set; } = [];
}
Example Output
var user = new User
{
Username = "john.doe",
Addresses = ["123 Main St, Apt 4B, New York, NY 10001"],
Preferences = new Dictionary<string, string>
{
{"Color", "Blue"}, {"Font", "Arial"}
}
};
Console.WriteLine(user.ToString()); // ToString() method automatically generated at compile time
// Output:
[User: Username = john.doe, Addresses = [123 Main St, Apt 4B, New York, NY 10001], Preferences = [{Color = Blue}, {Font = Arial}]
Handling Sensitive Data
Use the [SensitiveData]
attribute to mask sensitive information:
[GenerateToString]
public partial class User
{
public string Username { get; set; }
[SensitiveData] // Masks sensitive data - default value is '[REDACTED]'
public string Password { get; set; }
[SensitiveData("***")] // Custom masking values supported
public string CreditCardNumber { get; set; }
public List<string> Addresses { get; set; } = [];
public Dictionary<string, string> Preferences {get; set; } = [];
}
Example Output
var user = new User
{
Username = "john.doe",
Password = "MySecretPassword",
CreditCardNumber = "WouldntYouLikeToKnow",
Addresses = ["123 Main St, Apt 4B, New York, NY 10001"],
Preferences = new Dictionary<string, string>
{
{"Color", "Blue"}, {"Font", "Arial"}
}
}; // Using the example class above
Console.WriteLine(user.ToString());
// Output:
// [User: Username = john.doe, Password = [REDACTED], CreditCardNumber = ***, Addresses = [123 Main St, Apt 4B, New York, NY 10001], Preferences = [{Color = Blue}, {Font = Arial}]
You can also override the default value globally using the ToStringGeneratorRedactedValue
msbuild property:
<ToStringGeneratorRedactedValue>[MyNewRedactionValue]</ToStringGeneratorRedactedValue>
Whenever a masking value is not provided to SensitiveData
, this property's value will be used instead.
Attributes
The GenerateToString
and SensitiveData
attributes are injected into your project by the source generator by default.
This works fine for most use cases. However, in certain situations (notably when using [InternalsVisibleTo]
), the compiler
may end up with namespace collisions when generating these attributes across multiple projects in a single solution.
If you run into issues such as this, the required attributes are available to you to be referenced explicitly via
the Bcss.ToStringGenerator.Attributes
package.
dotnet add package Bcss.ToStringGenerator.Attributes
If you add an explicit reference to the Attributes package, you MUST also disable the automatic attribute generation within the source generator. To do so, set the following msbuild constant:
<DefineConstants>GENERATE_TO_STRING_EXCLUDE_GENERATED_ATTRIBUTES</DefineConstants>
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License - see LICENSE.md
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
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.0.0 | 140 | 5/20/2025 |