RSCG – BoolParameterGenerator
| name | BoolParameterGenerator |
| nuget | https://www.nuget.org/packages/BoolParameterGenerator/ |
| link | https://github.com/9swampy/BoolEnumGenerator |
| author | Justin Buchanan |
Generate boolean enum types
This is how you can use BoolParameterGenerator .
The code that you start with is
<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>
The code that you will use is
using BoolDemo; Console.WriteLine(IsValid.TrueValue);
using PrimS.BoolParameterGenerator;
namespace BoolDemo;
[GenerateBinaryEnum("TrueValue", "FalseValue", GenerateAssertionExtensions =false)]
//[GenerateBoolEnum("TrueValue", "FalseValue", GenerateAssertionExtensions = false)]
public partial class IsValid
{
}
The code that is generated is
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.
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/BoolParameterGenerator