GithubHelp home page GithubHelp logo

damien-o / enumconstraints.fody Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 122 KB

Validates enum property value.

Home Page: https://www.nuget.org/packages/EnumConstraints.Fody

License: MIT License

C# 98.41% Batchfile 1.59%
aop cecil enum fody ilweaving properties validation

enumconstraints.fody's Introduction

EnumConstraints.Fody logo EnumConstraints.Fody

NuGet Version NuGet Downloads .net Status Activity Coverage CodeQL License Chat on Gitter

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.