HasFlagExtension.Generator
2026.3.0
See the version list below for details.
dotnet add package HasFlagExtension.Generator --version 2026.3.0
NuGet\Install-Package HasFlagExtension.Generator -Version 2026.3.0
<PackageReference Include="HasFlagExtension.Generator" Version="2026.3.0" />
<PackageVersion Include="HasFlagExtension.Generator" Version="2026.3.0" />
<PackageReference Include="HasFlagExtension.Generator" />
paket add HasFlagExtension.Generator --version 2026.3.0
#r "nuget: HasFlagExtension.Generator, 2026.3.0"
#:package HasFlagExtension.Generator@2026.3.0
#addin nuget:?package=HasFlagExtension.Generator&version=2026.3.0
#tool nuget:?package=HasFlagExtension.Generator&version=2026.3.0
HasFlagExtension
HasFlagExtension is a simple source generator that creates HasFlag extension methods for flag enums.
Basic Usage
For dotnet versions lower than 10.0, the generator only generates the extension methods. For dotnet versions 10.0 and higher, it also generates extension properties that can be used for pattern matching.
Consider the following enum:
[Flags]
public enum MyFlags
{
None = 0,
FlagA = 1,
FlagB = 2,
FlagC = 4,
}
You can then use the extension methods like this:
MyFlags flags = MyFlags.FlagA | MyFlags.FlagB;
var a = flags.GetHasFlagA(); // true
var b = flags.GetHasFlagC(); // false
or when using dotnet 10.0 and higher:
MyFlags flags = MyFlags.FlagA | MyFlags.FlagB;
var a = flags.HasFlagA; // true
var b = flags.HasFlagC; // false
Pattern matching
If you are using dotnet 10.0 or higher, you can also use the extension properties for pattern matching:
MyFlags flags = MyFlags.FlagA | MyFlags.FlagB;
if (flags is { HasFlagA: true, HasFlagB: false })
{
// ...
}
HasFlagPrefixAttribute
The HasFlagPrefixAttribute can be used to specify a prefix for the generated
extension methods.
For instance, if we consider the enum from earlier and add the following line:
[HasFlagPrefix("Allow")]
then the generated extension methods will be named AllowFlagA, AllowFlagB...
FlagDisplayNameAttribute
The FlagDisplayNameAttribute can be used to specify what will the name of the flag be
when generating the extension methods.
For instance, if we consider the following enum with a different naming convention:
[Flags]
public enum MyFlags
{
[FlagDisplayName("None")]
NONE = 0,
[FlagDisplayName("FlagA")]
FLAG_A = 1,
[FlagDisplayName("FlagB")]
FLAG_B = 2,
[FlagDisplayName("FlagC")]
FLAG_C = 4,
}
then the generated extension methods will be named HasFlagA, HasFlagB...
EnumNamingAttribute
The EnumNamingAttribute can be used to specify what will the name of the flags be
when generating the extension methods. It can be used in combination with the
FlagDisplayNameAttribute and can be applied to the whole assembly and / or to
specific enums.
For instance, if we consider the following enum with a different naming convention:
[Flags]
[EnumNaming(NamingCase.TRAIN, NamingCase.Pascal)]
public enum MyFlags
{
FLAG_A,
FLAG_B,
FLAG_C,
FLAG_D
}
then the generated extension properties will be automatically named HasFlagA, HasFlagB...
Other Attributes
You can also exclude different flags from the generated extension methods by using the
ExcludeFlagAttribute attribute or even whole enums by using the ExcludeFlagEnumAttribute.
Enum Item Groups
You can also group flags and enum items into different groups using the
FlagGroupAttribute.
[FlagGroup("GroupA")]
[FlagGroup("GroupB")]
enum MyFlags
{
[FlagGroup("GroupA")]
FlagA,
[FlagGroup("GroupA")]
[FlagGroup("GroupB")]
FlagB,
[FlagGroup("GroupB")]
FlagC,
}
GetIsGroupA and GetIsGroupB extension methods will be generated for the enum (as
well as the extension properties for dotnet 10.0 and higher). This method will return
true if the value of the enum contains any of the items from the group. Example usage
can be seen below:
var a = MyFlags.FlagA;
Console.WriteLine(a.GetIsGroupA()); // true
Console.WriteLine(a.GetIsGroupB()); // false
Important: You need to first declare the group on the enum declaration for it to be recognized.
You can also specify a custom prefix for the generated extension methods:
[FlagGroup("GroupA", "Allows")]
[FlagGroup("GroupB")]
enum MyFlags
{
// ...
The code above will change the name of the GetIsGroupA extension
method to GetAllowsGroupA.
Installation
DotNet CLI
dotnet add package HasFlagExtension.Generator
NuGet CLI
Package-Install HasFlagExtension.Generator
.csproj
<ItemGroup>
<PackageReference Include="HasFlagExtension.Generator" Version="*"/>
</ItemGroup>
| 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 (>= 5.3.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on HasFlagExtension.Generator:
| Package | Downloads |
|---|---|
|
NeoKolors.Tui.Core
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.