Sefrone.Api.Auth.Generator 1.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

  • No dependencies.

Version Downloads Last updated
2.9.0 57 12/21/2025
2.8.9 119 12/16/2025
2.8.8 15 12/16/2025
2.8.7 60 12/14/2025
2.8.6 44 12/09/2025
2.8.5 40 12/09/2025
2.8.4 3 12/09/2025
2.8.3 5 12/08/2025
2.8.2 11 12/08/2025
2.8.1 69 12/03/2025
2.8.0 70 12/01/2025
2.7.9 31 12/01/2025
2.7.8 8 11/30/2025
2.7.7 24 11/30/2025
2.7.6 3 11/30/2025
2.7.5 1 11/30/2025
2.7.4 81 11/23/2025
2.7.3 9 11/23/2025
2.7.2 9 11/23/2025
2.7.1 18 11/22/2025
2.7.0 85 11/18/2025
2.6.9 20 11/15/2025
2.6.8 12 11/13/2025
2.6.7 3 11/13/2025
2.6.6 4 11/13/2025
2.6.5 5 11/12/2025
2.6.4 4 11/12/2025
2.6.3 149 10/19/2025
2.6.2 782 10/14/2025
2.6.1 8 10/12/2025
2.6.0 44 10/07/2025
2.5.9 113 10/05/2025
2.5.8 55 10/01/2025
2.5.7 301 09/21/2025
2.5.6 48 09/13/2025
2.5.5 270 08/26/2025
2.5.4 26 08/24/2025
2.5.3 31 08/24/2025
2.5.2 20 08/17/2025
2.5.1 101 08/09/2025
2.5.0 12 08/09/2025
2.4.9 29 07/14/2025
2.4.8 218 07/05/2025
2.4.7 403 06/30/2025
2.4.6 148 06/22/2025
2.4.5 158 06/09/2025
2.4.4 49 06/08/2025
2.4.3 9 06/07/2025
2.4.2 12 06/03/2025
2.4.1 104 06/02/2025
2.4.0 11 06/01/2025
2.3.9 223 05/12/2025
2.3.8 21 05/10/2025
2.3.7 18 05/10/2025
2.3.6 17 05/10/2025
2.3.5 15 05/10/2025
2.3.4 24 05/08/2025
2.3.3 18 05/06/2025
2.3.2 15 05/06/2025
2.3.1 15 05/06/2025
2.3.0 63 05/06/2025
2.2.9 53 05/05/2025
2.2.8 14 05/05/2025
2.2.7 16 05/04/2025
2.2.6 11 05/04/2025
2.2.5 9 05/03/2025
2.2.4 8 04/27/2025
2.2.3 12 04/27/2025
2.2.2 154 04/19/2025
2.2.1 12 04/19/2025
2.2.0 11 04/19/2025
2.1.9 9 04/19/2025
2.1.8 7 04/19/2025
2.1.7 60 04/07/2025
2.1.6 8 04/07/2025
2.1.5 9 04/07/2025
2.1.4 40 04/07/2025
2.1.3 10 04/07/2025
2.1.2 15 04/06/2025
2.1.1 16 03/25/2025
2.1.0 182 03/23/2025
2.0.9 13 03/23/2025
2.0.8 12 03/23/2025
2.0.7 15 03/22/2025
2.0.6 10 03/22/2025
2.0.5 259 02/23/2025
2.0.4 13 02/23/2025
2.0.3 13 02/22/2025
2.0.2 12 02/22/2025
2.0.1 100 02/19/2025
2.0.0 35 02/18/2025
1.9.9 11 02/18/2025
1.9.8 49 02/12/2025
1.9.7 108 02/05/2025
1.9.6 50 02/03/2025
1.9.5 14 02/03/2025
1.9.4 271 01/07/2025
1.9.3 236 12/25/2024
1.9.2 18 12/25/2024
1.9.1 34 12/22/2024
1.9.0 12 12/22/2024
1.8.9 17 12/21/2024
1.8.8 21 12/20/2024
1.8.7 18 12/19/2024
1.8.6 14 12/19/2024
1.8.5 13 12/19/2024
1.8.4 16 12/19/2024
1.8.3 24 12/18/2024
1.8.2 14 12/18/2024
1.8.1 14 12/18/2024
1.8.0 15 12/18/2024
1.7.9 17 12/18/2024
1.7.8 11 12/18/2024
1.7.7 23 12/16/2024
1.7.6 15 12/15/2024
1.7.5 19 12/13/2024
1.7.4 265 12/05/2024
1.7.3 27 11/25/2024
1.7.2 63 11/10/2024
1.7.1 74 10/28/2024
1.7.0 50 10/26/2024
1.6.9 87 10/20/2024
1.6.8 20 10/19/2024
1.6.7 52 09/15/2024
1.6.6 14 09/15/2024
1.6.5 268 09/08/2024
1.6.4 31 09/03/2024
1.6.3 14 09/03/2024
1.6.2 56 08/25/2024
1.6.1 26 08/25/2024
1.6.0 13 08/25/2024
1.5.9 16 08/25/2024
1.5.8 16 08/25/2024
1.5.7 52 08/13/2024
1.5.6 17 08/12/2024
1.5.5 13 08/12/2024
1.5.4 15 08/12/2024
1.5.3 12 08/12/2024
1.5.2 16 08/12/2024
1.5.1 17 08/12/2024
1.5.0 16 08/11/2024
1.4.9 30 07/28/2024
1.4.8 22 07/28/2024
1.4.7 19 07/14/2024
1.4.6 28 06/29/2024
1.4.5 122 06/05/2024
1.4.4 13 06/05/2024
1.4.3 28 06/04/2024
1.4.2 10 06/04/2024
1.4.1 16 06/03/2024
1.4.0 20 06/03/2024
1.3.9 16 06/02/2024
1.3.8 9 06/02/2024
1.3.7 11 06/02/2024
1.3.6 14 06/02/2024
1.3.5 10 06/02/2024
1.3.4 32 06/02/2024
1.3.3 12 06/02/2024
1.3.2 11 06/02/2024
1.3.1 12 06/01/2024
1.3.0 12 06/01/2024
1.2.9 12 06/01/2024
1.2.8 20 06/01/2024
1.2.7 18 05/31/2024
1.2.6 12 05/31/2024
1.2.5 23 05/31/2024
1.2.4 14 05/31/2024
1.2.2 10 05/31/2024
1.2.1 26 05/30/2024