EnumConstraints.Fody 0.1.2

dotnet add package EnumConstraints.Fody --version 0.1.2
NuGet\Install-Package EnumConstraints.Fody -Version 0.1.2
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="EnumConstraints.Fody" Version="0.1.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EnumConstraints.Fody --version 0.1.2
#r "nuget: EnumConstraints.Fody, 0.1.2"
#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.
// Install EnumConstraints.Fody as a Cake Addin
#addin nuget:?package=EnumConstraints.Fody&version=0.1.2

// Install EnumConstraints.Fody as a Cake Tool
#tool nuget:?package=EnumConstraints.Fody&version=0.1.2

EnumConstraints.Fody logo EnumConstraints.Fody

Chat on Gitter NuGet Status

Validates enum property value.

See Milestones for release notes.

This is an add-in for Fody

It is expected that all developers using Fody become a Patron on OpenCollective. See Licensing/Patron FAQ for more information.

Usage

See also Fody usage.

NuGet installation

Install the EnumConstraints.Fody NuGet package and update the Fody NuGet package:

PM> Install-Package Fody
PM> Install-Package EnumConstraints.Fody

The Install-Package Fody is required since NuGet always defaults to the oldest, and most buggy, version of any dependency.

Add to FodyWeavers.xml

Add <EnumConstraints/> to FodyWeavers.xml

<Weavers>
  <EnumConstraints/>
</Weavers>

What it does

  • Looks for all classes with Properties.
  • Generates a new implementations of Set and Get methods.
  • Replaces orginal implementations with the new ones.
  • The new implementations are using the originals implementations under the hood

Behavior

public enum Status
{
    Value1,
    Value2,
}

public class Sample
{
    public Status Status { get; set;}
}

var sample = new();

sample.Status = Status.Value1; // Valid

sample.Status = (Status)10; // Throws an InvalidEnumValueException

How it works

The orginal lowered code

public class Sample
{
    private Status <Status>k__BackingField;

    public Status Status
    {
        get; // Calls get_Status
        set; // Calls set_Status
    }

    public Status get_Status()
    {
        return <Status>k__BackingField;
    }
    public void set_Status(Status value)
    {
        <Status>k__BackingField = value;
    }
}

What gets compiled

public class Sample 
{
    private StringComparison <Status>k__BackingField;

    public Status Status
    {
        get; // Calls get_Status
        set; // Calls set_Status
    }

    public Status get_Status()
    {
        var value = get_Status_Unchecked();
        InvalidEnumValueException.ThrowIfInvalid(value);
        return value;
    }

    public void set_Status(Status value)
    {
        InvalidEnumValueException.ThrowIfInvalid(value);
        set_Status_Unchecked(value);
    }

    public Status get_Status_Unchecked
    {
        return <Status>k__BackingField;
    }
    public void set_Status_Unchecked(Status value)
    {
        <Status>k__BackingField = value;
    }
}

Icon

Checklist designed by Web Fonts.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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 is compatible.  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. 
.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 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.8

  • .NETStandard 2.0

  • .NETStandard 2.1

  • net6.0

  • net7.0

  • net8.0

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
0.1.2 223 2/19/2024
0.1.1 88 2/19/2024
0.1.0 98 2/18/2024
0.0.13 86 2/18/2024
0.0.12 73 2/18/2024
0.0.11 88 2/18/2024
0.0.10 73 2/18/2024
0.0.9 95 2/18/2024
0.0.8 88 2/18/2024
0.0.7 94 2/18/2024
0.0.6 87 2/18/2024
0.0.5 76 2/18/2024
0.0.4 94 2/18/2024
0.0.3 90 2/17/2024
0.0.2 81 2/17/2024
0.0.1 93 2/17/2024