HasFlagExtension 1.1.0.3

Suggested Alternatives

HasFlagExtension.Generator

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package HasFlagExtension --version 1.1.0.3
                    
NuGet\Install-Package HasFlagExtension -Version 1.1.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HasFlagExtension" Version="1.1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HasFlagExtension" Version="1.1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="HasFlagExtension" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add HasFlagExtension --version 1.1.0.3
                    
#r "nuget: HasFlagExtension, 1.1.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package HasFlagExtension@1.1.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=HasFlagExtension&version=1.1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=HasFlagExtension&version=1.1.0.3
                    
Install as a Cake Tool

<link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link href="https://fonts.googleapis.com/css2?family=Google+Sans+Code:ital,wght@0,300..800;1,300..800&family=Space+Grotesk:wght@300..700&family=Space+Mono:ital,wght@0,400;0,700;1,400;1,700&display=swap" rel="stylesheet">

<div align="center"> <h1 style="font-family: 'Space Grotesk', monospace">HasFlagExtension</h1> <p style="font-family: 'Space Grotesk', monospace"><b>by KryKom</b></p> </div>

<div align="center"> <p> <img src="https://img.shields.io/github/license/KryKomDev/HasFlagExtension?style=for-the-badge&labelColor=%235F6473&color=%23F2A0A0" alt="GitHub License" /> <a href="https://www.nuget.org/packages/HasFlagExtension"><img src="https://img.shields.io/nuget/v/HasFlagExtension?color=F0CA95&style=for-the-badge&labelColor=5F6473" alt="NuGet" /></a> <img src="https://img.shields.io/nuget/dt/HasFlagExtension?color=E3ED8A&style=for-the-badge&labelColor=5F6473" alt="NuGet Downloads" /> <img src="https://img.shields.io/github/actions/workflow/status/KryKomDev/HasFlagExtension/test.yml?style=for-the-badge&labelColor=%235F6473&color=%2395EC7D" alt="GitHub Actions Workflow Status" /> <img src="https://img.shields.io/badge/.NET-Standard2.0-7ACFDC?style=for-the-badge&labelColor=5F6473" alt=".NET Standard" /> </p> </div>

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 AllowHasFlagA, AllowHasFlagB...

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
{
    [DisplayName("None")]
    NONE = 0,
    
    [DisplayName("FlagA")]
    FLAG_A = 1,
    
    [DisplayName("FlagB")]
    FLAG_B = 2,
    
    [DisplayName("FlagC")]
    FLAG_C = 4,
}

then the generated extension methods will be 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.

Installation

DotNet CLI

dotnet add package HasFlagExtension
dotnet add package HasFlagExtension.Attributes

NuGet CLI

Package-Install HasFlagExtension
Package-Install HasFlagExtension.Attributes

.csproj

<ItemGroup>
    <PackageReference Include="HasFlagExtension" Version="1.1.0.1"/>
    <PackageReference Include="HasFlagExtension.Attributes" Version="1.1.0"/>
</ItemGroup>
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

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