BoolParameterGenerator by Justin Buchanan
- NuGet / site data
- Details
- ๐ซ Analyzer Rules Discouraging
boolParameters - โจ Features
- ๐ฆ Installation
- ๐ Usage
- ๐ Example Usage and Guidance
- ๐ Where to Find Generated Code
- ๐ฆ About the Shared Package
- ๐งช Confirmed Working Build/Contribution Setup
- โ ๏ธ Namespace Caveat
- ๐ License
- ๐งต See Also
- How to use
- Useful
NuGet / site dataโ
Detailsโ
Infoโ
Name: BoolParameterGenerator
BoolParameterGenerator is a Roslyn analyzer and source generator that automatically creates replacement types for boolean parameters in C# code. This improves code readability and maintainability by replacing ambiguous bools with descriptive types. See the README for usage and examples.
Author: Justin Buchanan
NuGet: https://www.nuget.org/packages/BoolParameterGenerator/
You can find more details at https://github.com/9swampy/BoolEnumGenerator
Authorโ
Justin Buchanan

Original Readmeโ
BoolParameterGenerator
BoolParameterGenerator is a Roslyn analyzer and source generator that automatically creates replacement types for boolean parameters in C# code. This improves readability and maintainability by replacing ambiguous bool parameters with strongly typed, descriptive alternatives.
๐ซ Analyzer Rules Discouraging bool Parametersโ
Using raw bool parameters in method signatures is often discouraged because it reduces code readability and clarity. Calls like SetFeature(true) can be ambiguous without context, making the code harder to understand and maintain.
Motivations for Avoiding bool Parametersโ
- Improved readability: Boolean parameters often obscure the intent of the method call.
- Explicit intent: Descriptive enums or strong types clarify the purpose.
- Better API discoverability: Strongly typed parameters enhance IntelliSense and documentation.
- Easier maintenance: Clearer code reduces bugs and onboarding time.
- Extensibility: Enums or wrappers allow for additional states beyond simple true/false.
Popular Analyzers and Their Rulesโ
-
SonarAnalyzer (SonarLint / SonarQube)
Rule: S1133 - Remove boolean parameters
Flags methods with boolean parameters to encourage more meaningful alternatives. -
Roslynator
Rule: RCS1155 - Avoid boolean parameters in methods
Suggests replacing boolean parameters with separate methods or enums for better readability. -
StyleCop Analyzers
While no specific rule bans boolean parameters, StyleCop encourages clear, descriptive API design that indirectly discourages ambiguous booleans.
How BoolParameterGenerator Addresses These Issuesโ
- Generates strongly typed, descriptive replacements for
boolparameters. - Improves code clarity, intent, and discoverability.
- Enables future extensibility beyond binary states.
- Helps maintain cleaner and more maintainable APIs.
โจ Featuresโ
- Replaces
boolparameters with source-generated binary types. - Supports generation of:
- Binary enums
- Struct-backed bool wrappers
- Seamless integration with IntelliSense and analyzers.
- Minimal configuration required.
๐ฆ Installationโ
Install the main analyzer package via NuGet:
<PackageReference Include="BoolParameterGenerator" Version="1.0.0" />
This will transitively install the required helper package BoolParameterGenerator.Shared.
โ Works in:
- .NET SDK-style projects
- Class libraries
- Console apps
- Unit test projects
๐ Usageโ
Annotate a partial class with one of the supported generator attributes:
using PrimS.BoolParameterGenerator;
[GenerateBinaryEnum("TrueValue", "FalseValue")]
public partial class MyBinaryEnum \{ }
[GenerateBoolEnum("TrueValue", "FalseValue")]
public partial class MyBoolEnum \{ }
๐ง Requirements:
- The class must be
partial. - The attribute arguments define the true/false semantics of the generated type.
๐ Example Usage and Guidanceโ
For detailed examples illustrating the benefits of BoolParameterGenerator, see the following:
- Good Examples: Caller IntelliSense โ CallerIntellisenseGoodExamples.cs
- Good Examples: Implementation Patterns โ ImplementationGoodExamples.cs
- Bad Examples: Caller IntelliSense Pitfalls โ CallerIntellisenseBadExamples.cs
- Bad Examples: Implementation Pitfalls โ ImplementationBadExamples.cs
These demonstrate why replacing raw bool parameters with strongly typed proxies enhances readability, API clarity, and maintainability.
๐ Where to Find Generated Codeโ
- Open your project in Visual Studio.
- Navigate to
Dependencies > Analyzers > BoolParameterGenerator. - Expand the node to find the generated
.g.csfiles (e.g.,MyBinaryEnum.g.cs).
โ ๏ธ If only Heartbeat.g.cs appears:
- Ensure your partial class is declared correctly.
- Verify that attribute arguments are valid.
- Rebuild the project to trigger generation.
๐ฆ About the Shared Packageโ
Although the attributes (GenerateBinaryEnum, GenerateBoolEnum) are defined in a separate package BoolParameterGenerator.Shared, you do not need to reference it manually โ it is installed transitively.
There isn't much to choose one type attribute over the other atm. Under the hood the implementation is quite different and we expect the BinaryEnum could prove advantageous; especially with respect to extending to a tri-state "boolean". This is a Work-In-Progres and we would be very happy to receive feedback on useCases that may deviate in interesting ways from our own expectations...
๐งช Confirmed Working Build/Contribution Setupโ
Check out the latest master branch then validate everything is wired correctly:
- Open only the generator projects (
BoolParameterGeneratorandBoolParameterGenerator.Shared) and the test project (BoolParameterGenerator.Pack.Tests). - Clean the solution.
- Rebuild
BoolParameterGenerator.Pack.Tests. - If BoolParameterGenerator analyzer references appear unresolved, try opening the file โ often Visual Studio will resolve them automatically when the file is activated.
- Rebuild again if necessary
- You'll hopefully see
BEG004diagnostics.
โ ๏ธ Namespace Caveatโ
If the triggering class and the generated class are in different namespaces, generation may fail silently. Ensure the partial class declaration and the generated file reside in the same namespace, or adjust your generator logic to support custom namespaces.
๐ Licenseโ
MIT โ essentially use however you like, just don't sue me if it doesn't work out!
๐งต See Alsoโ
Aboutโ
Generate boolean enum types
How to useโ
Example (source csproj, source files)โ
- CSharp Project
- Program.cs
- IsValid.cs
This is the CSharp Project that references BoolParameterGenerator
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BoolParameterGenerator" Version="0.5.0" />
</ItemGroup>
</Project>
This is the use of BoolParameterGenerator in Program.cs
using BoolDemo;
Console.WriteLine(IsValid.TrueValue);
This is the use of BoolParameterGenerator in IsValid.cs
using PrimS.BoolParameterGenerator;
namespace BoolDemo;
[GenerateBinaryEnum("TrueValue", "FalseValue", GenerateAssertionExtensions =false)]
//[GenerateBoolEnum("TrueValue", "FalseValue", GenerateAssertionExtensions = false)]
public partial class IsValid
{
}
Generated Filesโ
Those are taken from $(BaseIntermediateOutputPath)\GX
- IsValid.g.cs
- GeneratorHeartbeat.g.cs
namespace BoolDemo;
#nullable enable
using System;
using PrimS.BoolParameterGenerator;
public partial class IsValid : SmartEnumWrapper<IsValid, BinaryEnum>, IEquatable<BinaryEnum>, IComparable<BinaryEnum>, IEquatable<BinaryEnumWrapper<IsValid, BinaryEnum>>, IComparable<BinaryEnumWrapper<IsValid, BinaryEnum>>
{
public static readonly IsValid FalseValue = new IsValid(nameof(FalseValue), BinaryEnum.False);
public static readonly IsValid TrueValue = new IsValid(nameof(TrueValue), BinaryEnum.True);
public bool BoolValue => ProxyValue == BinaryEnum.True;
public static IsValid FromValue(BinaryEnum value) => value switch
{
BinaryEnum.False => FalseValue,
BinaryEnum.True => TrueValue,
_ => throw new ArgumentOutOfRangeException(nameof(value), value, "Unhandled value for IsValid")
};
public static IsValid FromValue(bool value) => value switch
{
false => FalseValue,
true => TrueValue
};
private IsValid(string name, BinaryEnum value) : base(name, value)
\{ }
public static implicit operator bool(IsValid value)
{
return value.Value == BinaryEnum.True;
}
public static implicit operator IsValid(bool value) => value ? TrueValue : FalseValue;
public static bool operator ==(IsValid left, IsValid right) => left.Value.Value == right.Value.Value;
public static bool operator ==(IsValid left, bool right) => left.Value == (right ? BinaryEnum.True : BinaryEnum.False);
public static bool operator ==(IsValid left, int right) => (int)left.Value.Value == right;
public static bool operator ==(bool left, IsValid right) => (left ? 1 : 0) == (int)right.Value.Value;
public static bool operator !=(IsValid left, IsValid right) => left.Value.Value != right.Value.Value;
public static bool operator !=(IsValid left, bool right) => (int)left.Value.Value != (right ? 1 : 0);
public static bool operator !=(IsValid left, int right) => (int)left.Value.Value != right;
public static bool operator !=(bool left, IsValid right) => (left ? 1 : 0) != (int)right.Value.Value;
public static implicit operator IsValid(BinaryEnum value) => IsValid.FromValue(value);
public override bool Equals(object obj) => obj is IsValid other && this == other;
public override int GetHashCode() => Value.GetHashCode();
public bool Equals(BinaryEnum other) => Equals(FromValue(other));
public int CompareTo(BinaryEnum other) => CompareTo(FromValue(other));
public int CompareTo(BinaryEnumWrapper<IsValid, BinaryEnum>? other) => Value.CompareTo(other?.Value);
public bool Equals(BinaryEnumWrapper<IsValid, BinaryEnum>? other) => Value.Equals(other?.Value);
}
// BoolEnumGenerator Generator ran successfully but found no GenerateBoolEnumAttribute to process.
Usefulโ
Download Example (.NET C#)โ
Share BoolParameterGeneratorโ
https://ignatandrei.github.io/RSCG_Examples/v2/docs/BoolParameterGenerator
Category "Bool" has the following generators:โ
1 BoolParameterGenerator
2025-12-11