All Projects → TylerBrinkley → Enums.net

TylerBrinkley / Enums.net

Licence: mit
Enums.NET is a high-performance type-safe .NET enum utility library

Programming Languages

csharp
926 projects
enum
40 projects

Projects that are alternatives of or similar to Enums.net

Dotnetworkqueue
A work queue for dot.net with SQL server, SQLite, Redis and PostGreSQL backends
Stars: ✭ 58 (-94.96%)
Mutual labels:  dotnet-core
Devchatterbot
Stars: ✭ 60 (-94.79%)
Mutual labels:  dotnet-core
Stl.fusion.samples
A collection of samples for Fusion library: https://github.com/servicetitan/Stl.Fusion
Stars: ✭ 65 (-94.35%)
Mutual labels:  dotnet-core
Community Cluster
OpenFaaS Cloud Cluster for Community
Stars: ✭ 59 (-94.87%)
Mutual labels:  dotnet-core
Dotnet Bundle
MSBuild task and CLI tools for bundling .NET Core projects into MacOS applications (.app)
Stars: ✭ 59 (-94.87%)
Mutual labels:  dotnet-core
Ionic Example App
A Ionic Example App (previously known as ionic 2 examples). Contains different examples on how to use the Ionic Framework
Stars: ✭ 61 (-94.7%)
Mutual labels:  dotnet-core
Yarn.msbuild
MSBuild integration for the Yarn package manager.
Stars: ✭ 57 (-95.05%)
Mutual labels:  dotnet-core
Drinkandgo
A simple eCommerce App build on top of Asp.Net Core MVC framework.
Stars: ✭ 65 (-94.35%)
Mutual labels:  dotnet-core
Pioneer Console Boilerplate
Dependency injection, logging and configuration in a .NET Core console application.
Stars: ✭ 60 (-94.79%)
Mutual labels:  dotnet-core
Skater .net Obfuscator
Skater .NET Obfuscator is an obfuscation tool for .NET code protection. It implements all known software protection techniques and obfuscation algorithms.
Stars: ✭ 64 (-94.44%)
Mutual labels:  dotnet-core
Developing Solutions Azure Exam
This repository contains resources for the Exam AZ-203: Developing Solutions for Microsoft Azure. You can find direct links to resources and and practice resources to test yourself ☁️🎓📚
Stars: ✭ 59 (-94.87%)
Mutual labels:  dotnet-core
Aspnet Core Clean Arch
It is a clean architecture project template which is based on hexagonal-architecture principles built with .Net core.
Stars: ✭ 60 (-94.79%)
Mutual labels:  dotnet-core
Icanpay.donet
统一支付网关。支持NET46和NETSTANDARD2_0。支持支付宝,微信,银联支付渠道通过Web,App,Wap,QRCode方式支付。简化订单的创建、查询、退款跟接收网关返回的支付通知等功能
Stars: ✭ 62 (-94.61%)
Mutual labels:  dotnet-core
Virtualizingwrappanel
Implementation of a VirtualizingWrapPanel for WPF running .NET Framework 4.5+ or .NET Core 3.0+
Stars: ✭ 59 (-94.87%)
Mutual labels:  dotnet-core
Waypoint
Opinionated solution template for building F# OSS libraries and tools.
Stars: ✭ 65 (-94.35%)
Mutual labels:  dotnet-core
Fakeiteasy
The easy mocking library for .NET
Stars: ✭ 1,092 (-5.13%)
Mutual labels:  dotnet-core
Aspnetboilerplate Core Ng
Tutorial for ASP.NET Boilerplate Core + Angular
Stars: ✭ 61 (-94.7%)
Mutual labels:  dotnet-core
Poke
A powerful reflection module for powershell.
Stars: ✭ 66 (-94.27%)
Mutual labels:  dotnet-core
Cppast.codegen
An extensible library providing C# PInvoke codegen from C/C++ files for .NET
Stars: ✭ 65 (-94.35%)
Mutual labels:  dotnet-core
Lambda Native
Make .NET AWS Lambda functions start 10x faster using LambdaNative.
Stars: ✭ 64 (-94.44%)
Mutual labels:  dotnet-core

GitHub last commit (master) NuGet Version NuGet Downloads Build status

v4.0 Changes

Removed NonGenericEnums, NonGenericFlagEnums, UnsafeEnums, and UnsafeFlagEnums classes which were deprecated in v3.0 and also removed all other deprecated methods in an effort to slim the library size down. It is recommended if upgrading from 2.x and below to update to 3.x first and follow the warnings to migrate any code that's using deprecated methods and classes. Also, a dependency on the System.Runtime.CompilerServices.Unsafe package was added for the .NET 4.5 target in order to remove a build dependency on Fody.

v3.0 Changes

One of the major changes for v3.0 is the deprecation of the NonGenericEnums, NonGenericFlagEnums, UnsafeEnums, and UnsafeFlagEnums classes whose methods have been added to the Enums and FlagEnums classes to better match System.Enum and provide better discoverability. To help you migrate your code to using the new methods I have created the C# roslyn analyzer Enums.NET.Analyzer which provides a code fix to migrate your usages of the non-generic and unsafe methods to the new methods.

Enums.NET

Enums.NET is a high-performance type-safe .NET enum utility library which provides many operations as convenient extension methods. It is compatible with .NET Framework 4.5+ and .NET Standard 1.0+.

What's wrong with System.Enum

  1. Nearly all of Enum's static methods are non-generic leading to the following issues.
    • Requires the enum type to be explicitly specified as an argument and requires invocation using static method syntax such as Enum.IsDefined(typeof(ConsoleColor), value) instead of what should be value.IsDefined().
    • Requires casting/unboxing for methods with an enum return value, eg. ToObject, Parse, and GetValues.
    • Requires boxing for methods with enum input parameters losing type-safety, eg. IsDefined and GetName.
  2. Support for flag enums is limited to just the HasFlag method which isn't type-safe, is inefficient, and is ambiguous as to whether it determines if the value has all or any of the specified flags. It's all by the way.
  3. Most of its methods use reflection on each call without any sort of caching causing poor performance.
  4. The pattern to associate extra data with an enum member using Attributes is not supported and instead requires users to manually retrieve the Attributes via reflection. This pattern is commonly used on enum members with the DescriptionAttribute, EnumMemberAttribute, and DisplayAttribute.

Enums.NET solves all of these issues and more.

Enums.NET Demo

using System;
using System.Linq;
using EnumsNET;
using NUnit.Framework;
using DescriptionAttribute = System.ComponentModel.DescriptionAttribute;

[TestFixture]
class EnumsNETDemo
{
    // Enum definitions at bottom

    [Test]
    public void Enumerate()
    {
        var count = 0;
        // Retrieves all enum members in increasing value order
        foreach (var member in Enums.GetMembers<NumericOperator>())
        {
            NumericOperator value = member.Value;
            string name = member.Name;
            AttributeCollection attributes = member.Attributes;
            ++count;
        }
        Assert.AreEqual(8, count);

        count = 0;
        // Retrieves distinct values in increasing value order
        foreach (var value in Enums.GetValues<NumericOperator>(EnumMemberSelection.Distinct))
        {
            string name = value.GetName();
            AttributeCollection attributes = value.GetAttributes();
            ++count;
        }
        Assert.AreEqual(6, count);
    }

    [Test]
    public void FlagEnumOperations()
    {
        // HasAllFlags
        Assert.IsTrue((DaysOfWeek.Monday | DaysOfWeek.Wednesday | DaysOfWeek.Friday).HasAllFlags(DaysOfWeek.Monday | DaysOfWeek.Wednesday));
        Assert.IsFalse(DaysOfWeek.Monday.HasAllFlags(DaysOfWeek.Monday | DaysOfWeek.Wednesday));

        // HasAnyFlags
        Assert.IsTrue(DaysOfWeek.Monday.HasAnyFlags(DaysOfWeek.Monday | DaysOfWeek.Wednesday));
        Assert.IsFalse((DaysOfWeek.Monday | DaysOfWeek.Wednesday).HasAnyFlags(DaysOfWeek.Friday));

        // CombineFlags ~ bitwise OR
        Assert.AreEqual(DaysOfWeek.Monday | DaysOfWeek.Wednesday, DaysOfWeek.Monday.CombineFlags(DaysOfWeek.Wednesday));
        Assert.AreEqual(DaysOfWeek.Monday | DaysOfWeek.Wednesday | DaysOfWeek.Friday, FlagEnums.CombineFlags(DaysOfWeek.Monday, DaysOfWeek.Wednesday, DaysOfWeek.Friday));

        // CommonFlags ~ bitwise AND
        Assert.AreEqual(DaysOfWeek.Monday, DaysOfWeek.Monday.CommonFlags(DaysOfWeek.Monday | DaysOfWeek.Wednesday));
        Assert.AreEqual(DaysOfWeek.None, DaysOfWeek.Monday.CommonFlags(DaysOfWeek.Wednesday));

        // RemoveFlags
        Assert.AreEqual(DaysOfWeek.Wednesday, (DaysOfWeek.Monday | DaysOfWeek.Wednesday).RemoveFlags(DaysOfWeek.Monday));
        Assert.AreEqual(DaysOfWeek.None, (DaysOfWeek.Monday | DaysOfWeek.Wednesday).RemoveFlags(DaysOfWeek.Monday | DaysOfWeek.Wednesday));

        // GetFlags, splits out the individual flags in increasing significance bit order
        var flags = DaysOfWeek.Weekend.GetFlags();
        Assert.AreEqual(2, flags.Count);
        Assert.AreEqual(DaysOfWeek.Sunday, flags[0]);
        Assert.AreEqual(DaysOfWeek.Saturday, flags[1]);
    }

    [Test]
    public new void ToString()
    {
        // AsString, equivalent to ToString
        Assert.AreEqual("Equals", NumericOperator.Equals.AsString());
        Assert.AreEqual("-1", ((NumericOperator)(-1)).AsString());

        // GetName
        Assert.AreEqual("Equals", NumericOperator.Equals.GetName());
        Assert.IsNull(((NumericOperator)(-1)).GetName());

        // Get description
        Assert.AreEqual("Is", NumericOperator.Equals.AsString(EnumFormat.Description));
        Assert.IsNull(NumericOperator.LessThan.AsString(EnumFormat.Description));

        // Get description if applied, otherwise the name
        Assert.AreEqual("LessThan", NumericOperator.LessThan.AsString(EnumFormat.Description, EnumFormat.Name));
    }

    [Test]
    public void Validate()
    {
        // Standard Enums, checks is defined
        Assert.IsTrue(NumericOperator.LessThan.IsValid());
        Assert.IsFalse(((NumericOperator)20).IsValid());

        // Flag Enums, checks is valid flag combination or is defined
        Assert.IsTrue((DaysOfWeek.Sunday | DaysOfWeek.Wednesday).IsValid());
        Assert.IsFalse((DaysOfWeek.Sunday | DaysOfWeek.Wednesday | ((DaysOfWeek)(-1))).IsValid());

        // Custom validation through IEnumValidatorAttribute<TEnum>
        Assert.IsTrue(DayType.Weekday.IsValid());
        Assert.IsTrue((DayType.Weekday | DayType.Holiday).IsValid());
        Assert.IsFalse((DayType.Weekday | DayType.Weekend).IsValid());
    }

    [Test]
    public void CustomEnumFormat()
    {
        EnumFormat symbolFormat = Enums.RegisterCustomEnumFormat(member => member.Attributes.Get<SymbolAttribute>()?.Symbol);
        Assert.AreEqual(">", NumericOperator.GreaterThan.AsString(symbolFormat));
        Assert.AreEqual(NumericOperator.LessThan, Enums.Parse<NumericOperator>("<", ignoreCase: false, symbolFormat));
    }

    [Test]
    public void Attributes()
    {
        Assert.AreEqual("!=", NumericOperator.NotEquals.GetAttributes().Get<SymbolAttribute>().Symbol);
        Assert.IsTrue(Enums.GetMember<NumericOperator>("GreaterThanOrEquals").Attributes.Has<PrimaryEnumMemberAttribute>());
        Assert.IsFalse(NumericOperator.LessThan.GetAttributes().Has<DescriptionAttribute>());
    }

    [Test]
    public void Parsing()
    {
        Assert.AreEqual(NumericOperator.GreaterThan, Enums.Parse<NumericOperator>("GreaterThan"));
        Assert.AreEqual(NumericOperator.NotEquals, Enums.Parse<NumericOperator>("1"));
        Assert.AreEqual(NumericOperator.Equals, Enums.Parse<NumericOperator>("Is", ignoreCase: false, EnumFormat.Description));

        Assert.AreEqual(DaysOfWeek.Monday | DaysOfWeek.Wednesday, Enums.Parse<DaysOfWeek>("Monday, Wednesday"));
        Assert.AreEqual(DaysOfWeek.Tuesday | DaysOfWeek.Thursday, FlagEnums.ParseFlags<DaysOfWeek>("Tuesday | Thursday", ignoreCase: false, delimiter: "|"));
    }

    enum NumericOperator
    {
        [Symbol("="), Description("Is")]
        Equals,
        [Symbol("!="), Description("Is not")]
        NotEquals,
        [Symbol("<")]
        LessThan,
        [Symbol(">="), PrimaryEnumMember] // PrimaryEnumMember indicates enum member as primary duplicate for extension methods
        GreaterThanOrEquals,
        NotLessThan = GreaterThanOrEquals,
        [Symbol(">")]
        GreaterThan,
        [Symbol("<="), PrimaryEnumMember]
        LessThanOrEquals,
        NotGreaterThan = LessThanOrEquals
    }

    [AttributeUsage(AttributeTargets.Field)]
    class SymbolAttribute : Attribute
    {
        public string Symbol { get; }

        public SymbolAttribute(string symbol)
        {
            Symbol = symbol;
        }
    }

    [Flags]
    enum DaysOfWeek
    {
        None = 0,
        Sunday = 1,
        Monday = 2,
        Tuesday = 4,
        Wednesday = 8,
        Thursday = 16,
        Friday = 32,
        Weekdays = Monday | Tuesday | Wednesday | Thursday | Friday,
        Saturday = 64,
        Weekend = Sunday | Saturday,
        All = Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday
    }

    [Flags, DayTypeValidator]
    enum DayType
    {
        Weekday = 1,
        Weekend = 2,
        Holiday = 4
    }

    [AttributeUsage(AttributeTargets.Enum)]
    class DayTypeValidatorAttribute : Attribute, IEnumValidatorAttribute<DayType>
    {
        public bool IsValid(DayType value) => value.GetFlagCount(DayType.Weekday | DayType.Weekend) == 1 && FlagEnums.IsValidFlagCombination(value);
    }
}

Performance

Results from running the PerformanceTestConsole BenchmarkDotNet application.

Interface

See fuget for exploring the interface.

Credits

Inspired by Jon Skeet's Unconstrained Melody.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].