Sefrone.Api.Auth.Generator 2.9.1

Sefrone dotnet api packages

RequiredIfEnumValueAnalyzer Usage Example

This example demonstrates how to use the custom RequiredIfEnumValueAnalyzer in a .NET project to enforce the rule that a nullable property must be set to a non-null value when an enum array contains a specified value.

Setup

First, define the FeatureSet enum and create a class that uses the [RequiredIfEnumValue] attribute.

1. Define the FeatureSet Enum

public enum FeatureSet
{
    Basic,
    Premium,
    Advanced
}

2. Define the RequiredIfEnumValueAttribute

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class RequiredIfEnumValueAttribute : Attribute
{
    public string EnumPropertyName { get; }
    public string DependentPropertyName { get; }
    public int RequiredEnumValue { get; }

    public RequiredIfEnumValueAttribute(string enumPropertyName, string dependentPropertyName, int requiredEnumValue)
    {
        EnumPropertyName = enumPropertyName;
        DependentPropertyName = dependentPropertyName;
        RequiredEnumValue = requiredEnumValue;
    }
}

3. Apply the [RequiredIfEnumValue] Attribute to a Class

[RequiredIfEnumValue("Features", "Data", (int)FeatureSet.Premium)]
public partial class MyFeatureClass
{
    public FeatureSet[] Features { get; set; }
    public string? Data { get; set; }

    public MyFeatureClass(FeatureSet[] features, string? data)
    {
        Features = features;
        Data = data;
    }
}

Behavior of the Analyzer

The analyzer (RequiredIfEnumValueAnalyzer) will check the following:

  • The MyFeatureClass has the [RequiredIfEnumValue] attribute applied.
  • The Features property is an array of the FeatureSet enum type.
  • The Data property is nullable.
  • It will report a diagnostic if the Data property is nullable, indicating that it should not be null when the Features array contains the value FeatureSet.Premium.

Example Usage in a Project

Correct Usage (No Diagnostic)

public class Program
{
    public static void Main()
    {
        var validInstance = new MyFeatureClass(new[] { FeatureSet.Basic, FeatureSet.Premium }, "Non-null data");
        // No diagnostic error here because Data is set to a non-null value.
    }
}

Incorrect Usage (Triggers a Diagnostic)

public class Program
{
    public static void Main()
    {
        var invalidInstance = new MyFeatureClass(new[] { FeatureSet.Premium }, null);
        // This will trigger a diagnostic error from the analyzer because the Data property is null
        // and the Features array contains FeatureSet.Premium.
    }
}

Diagnostic Output

When the analyzer detects the issue in the "Incorrect Usage" example, it will produce a compile-time error similar to:

Error REQUIRED_NULL_CHECK: Property 'Data' should not be null when 'Features' contains the specified enum value.

The error message helps the developer understand that the Data property needs to be set to a non-null value when the Features array includes FeatureSet.Premium.

How to Add the Analyzer to the Project

  1. Add the Analyzer Project to Your Solution:

    • Create a separate analyzer project for the RequiredIfEnumValueAnalyzer, or use an existing one.
    • Build the analyzer project to produce a .dll file for the analyzer.
  2. Reference the Analyzer in the Target Project:

    • Add a reference to the analyzer .dll in the project where you want to apply the rule.
    • You can do this by using NuGet, directly referencing the .dll, or adding the analyzer as a project reference.
  3. Enable the Analyzer in the Target Project:

    • When you build your project, the custom analyzer will automatically run and produce diagnostics based on your defined rules.

By following these steps, you can enforce compile-time validation rules based on custom conditions, providing immediate feedback to developers when they violate the rules defined by the [RequiredIfEnumValue] attribute.

No packages depend on Sefrone.Api.Auth.Generator.

.NET Standard 2.0

Version Downloads Last updated
2.9.2 222 01/11/2026
2.9.1 2 01/11/2026
2.9.0 422 12/21/2025
2.8.9 201 12/16/2025
2.8.8 18 12/16/2025
2.8.7 62 12/14/2025
2.8.6 47 12/09/2025
2.8.5 42 12/09/2025
2.8.4 5 12/09/2025
2.8.3 7 12/08/2025
2.8.2 13 12/08/2025
2.8.1 71 12/03/2025
2.8.0 72 12/01/2025
2.7.9 33 12/01/2025
2.7.8 10 11/30/2025
2.7.7 26 11/30/2025
2.7.6 5 11/30/2025
2.7.5 3 11/30/2025
2.7.4 83 11/23/2025
2.7.3 11 11/23/2025
2.7.2 11 11/23/2025
2.7.1 20 11/22/2025
2.7.0 87 11/18/2025
2.6.9 22 11/15/2025
2.6.8 14 11/13/2025
2.6.7 5 11/13/2025
2.6.6 6 11/13/2025
2.6.5 7 11/12/2025
2.6.4 6 11/12/2025
2.6.3 151 10/19/2025
2.6.2 848 10/14/2025
2.6.1 10 10/12/2025
2.6.0 46 10/07/2025
2.5.9 115 10/05/2025
2.5.8 57 10/01/2025
2.5.7 303 09/21/2025
2.5.6 50 09/13/2025
2.5.5 273 08/26/2025
2.5.4 28 08/24/2025
2.5.3 33 08/24/2025
2.5.2 22 08/17/2025
2.5.1 103 08/09/2025
2.5.0 14 08/09/2025
2.4.9 31 07/14/2025
2.4.8 221 07/05/2025
2.4.7 405 06/30/2025
2.4.6 150 06/22/2025
2.4.5 161 06/09/2025
2.4.4 51 06/08/2025
2.4.3 11 06/07/2025
2.4.2 14 06/03/2025
2.4.1 106 06/02/2025
2.4.0 13 06/01/2025
2.3.9 225 05/12/2025
2.3.8 23 05/10/2025
2.3.7 20 05/10/2025
2.3.6 19 05/10/2025
2.3.5 17 05/10/2025
2.3.4 27 05/08/2025
2.3.3 20 05/06/2025
2.3.2 17 05/06/2025
2.3.1 17 05/06/2025
2.3.0 65 05/06/2025
2.2.9 55 05/05/2025
2.2.8 16 05/05/2025
2.2.7 18 05/04/2025
2.2.6 13 05/04/2025
2.2.5 11 05/03/2025
2.2.4 10 04/27/2025
2.2.3 14 04/27/2025
2.2.2 157 04/19/2025
2.2.1 14 04/19/2025
2.2.0 13 04/19/2025
2.1.9 11 04/19/2025
2.1.8 9 04/19/2025
2.1.7 62 04/07/2025
2.1.6 10 04/07/2025
2.1.5 11 04/07/2025
2.1.4 42 04/07/2025
2.1.3 13 04/07/2025
2.1.2 17 04/06/2025
2.1.1 18 03/25/2025
2.1.0 185 03/23/2025
2.0.9 15 03/23/2025
2.0.8 14 03/23/2025
2.0.7 17 03/22/2025
2.0.6 12 03/22/2025
2.0.5 261 02/23/2025
2.0.4 15 02/23/2025
2.0.3 15 02/22/2025
2.0.2 14 02/22/2025
2.0.1 102 02/19/2025
2.0.0 38 02/18/2025
1.9.9 14 02/18/2025
1.9.8 51 02/12/2025
1.9.7 111 02/05/2025
1.9.6 52 02/03/2025
1.9.5 16 02/03/2025
1.9.4 273 01/07/2025
1.9.3 238 12/25/2024
1.9.2 20 12/25/2024
1.9.1 36 12/22/2024
1.9.0 15 12/22/2024
1.8.9 19 12/21/2024
1.8.8 23 12/20/2024
1.8.7 20 12/19/2024
1.8.6 16 12/19/2024
1.8.5 15 12/19/2024
1.8.4 18 12/19/2024
1.8.3 26 12/18/2024
1.8.2 17 12/18/2024
1.8.1 16 12/18/2024
1.8.0 18 12/18/2024
1.7.9 19 12/18/2024
1.7.8 14 12/18/2024
1.7.7 25 12/16/2024
1.7.6 17 12/15/2024
1.7.5 21 12/13/2024
1.7.4 267 12/05/2024
1.7.3 30 11/25/2024
1.7.2 66 11/10/2024
1.7.1 76 10/28/2024
1.7.0 52 10/26/2024
1.6.9 90 10/20/2024
1.6.8 22 10/19/2024
1.6.7 54 09/15/2024
1.6.6 16 09/15/2024
1.6.5 271 09/08/2024
1.6.4 33 09/03/2024
1.6.3 16 09/03/2024
1.6.2 58 08/25/2024
1.6.1 28 08/25/2024
1.6.0 15 08/25/2024
1.5.9 18 08/25/2024
1.5.8 18 08/25/2024
1.5.7 55 08/13/2024
1.5.6 20 08/12/2024
1.5.5 15 08/12/2024
1.5.4 18 08/12/2024
1.5.3 15 08/12/2024
1.5.2 18 08/12/2024
1.5.1 19 08/12/2024
1.5.0 18 08/11/2024
1.4.9 33 07/28/2024
1.4.8 24 07/28/2024
1.4.7 22 07/14/2024
1.4.6 31 06/29/2024
1.4.5 124 06/05/2024
1.4.4 15 06/05/2024
1.4.3 31 06/04/2024
1.4.2 12 06/04/2024
1.4.1 18 06/03/2024
1.4.0 22 06/03/2024
1.3.9 18 06/02/2024
1.3.8 12 06/02/2024
1.3.7 14 06/02/2024
1.3.6 16 06/02/2024
1.3.5 13 06/02/2024
1.3.4 34 06/02/2024
1.3.3 14 06/02/2024
1.3.2 13 06/02/2024
1.3.1 14 06/01/2024
1.3.0 15 06/01/2024
1.2.9 14 06/01/2024
1.2.8 22 06/01/2024
1.2.7 20 05/31/2024
1.2.6 15 05/31/2024
1.2.5 25 05/31/2024
1.2.4 16 05/31/2024
1.2.2 13 05/31/2024
1.2.1 28 05/30/2024