Git Product home page Git Product logo

h7p.enum.boosterpack's Introduction

What is Enum Boosterpack?

Enum Boosterpack is a set of source generators which creates fast extension methods for your enums.

  • AsString() - ToString() on enum-values is slow, AsString() is more than 17x faster on .NET 5, and more than 200x faster on .NET 4.8.
  • AutoDescriptor - Creates a GetDescription() extension method that returns the value from enum-values decorated with a Description-attribute, which is more than 950x times faster on .NET 5.0, and more than 1650x faster on .NET 4.8 than using reflection.

Unlike ToString() and extracting the description-value using reflection, AsString() and GetDescription() doesn't allocate.

Limitations

The following limitations apply:

  • No support for flags yet
  • Supports public and internal enums only

How do I get started?

Enum Boosterpack will automatically identify and create AsString() extension methods for your public and internal enums. Declare an enum:

internal enum Color
{
    Red,
    Green
}   

Then in your application code, call AsString() instead of ToString() on your enum-value:

var redString = Color.Red.AsString();

To generate GetDescription() extension methods, add a Describable-attribute to your enums. Make sure all enum-values have a Description-attribute:

using H7P.AutoDescriptor;

[Describable]
internal enum Color
{
    [Description("Red as a rose")]
    Red,

    [Description("Green as the grass")]
    Green
}   

Then in your application code, call GetDescription() on the enum-value:

var redDescription = Color.Red.GetDescription();

Performance

To verify the performance claims, run the provided benchmarks in the project "H7P.Enum.BoosterPack.Benchmarks".

The benchmarks use BenchmarkDotNet, and require that you compile the benchmark-project in release-mode and run them from the command line.

If you have any suggestions on how to improve the performance even further, please file an issue with some example code, or even better open a pull request.

Where can I get it?

First, install NuGet. Then, install Enum Boosterpack from the package manager console:

PM> Install-Package H7P.Enum.Boosterpack

Do you have an issue?

File an issue above.

License, etc.

Enum Boosterpack is Copyright © 2021 Fred Myklebust and other contributors under the MIT license.

h7p.enum.boosterpack's People

Contributors

fredmyklebust avatar

Watchers

James Cloos avatar  avatar

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.