GithubHelp home page GithubHelp logo

samjib / enum.source.generator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from engrajabi/enum.source.generator

0.0 0.0 0.0 180 KB

A C# source generator to create an enumeration class from an enum type. With this package, you can work on enums very, very fast without using reflection.

License: MIT License

C# 97.70% Batchfile 2.30%

enum.source.generator's Introduction

GitHub license Nuget Nuget

Supernova.Enum.Generators

The best Source Generator for working with enums in C#

A C# source generator to create an enumeration class from an enum type. With this package, you can work on enums very, very fast without using reflection.

Package - Supernova.Enum.Generators

Add the package to your application using

dotnet add package Supernova.Enum.Generators

Adding the package will automatically add a marker attribute, [EnumGenerator], to your project.

To use the generator, add the [EnumGenerator] attribute to an enum. For example:

[EnumGenerator]
public enum UserTypeTest
{
    [Display(Name = "مرد")]
    Men,

    [Display(Name = "زن")]
    Women,

    //[Display(Name = "نامشخص")]
    None
}

This will generate a class called EnumNameEnumExtensions (UserTypeTest + EnumExtensions), which contains a number of helper methods. For example:

    public static class UserTypeTestEnumExtensions
    {
        public static string ToStringFast(this UnitTests.UserTypeTest states)
        {
            return states switch
            {
                UnitTests.UserTypeTest.Men => nameof(UnitTests.UserTypeTest.Men),
                UnitTests.UserTypeTest.Women => nameof(UnitTests.UserTypeTest.Women),
                UnitTests.UserTypeTest.None => nameof(UnitTests.UserTypeTest.None),
                _ => throw new ArgumentOutOfRangeException(nameof(states), states, null)
            };
        }
        public static bool IsDefinedFast(UnitTests.UserTypeTest states)
        {
            return states switch
            {
                UnitTests.UserTypeTest.Men => true,
                UnitTests.UserTypeTest.Women => true,
                UnitTests.UserTypeTest.None => true,
                _ => throw new ArgumentOutOfRangeException(nameof(states), states, null)
            };
        }
        public static bool IsDefinedFast(string states)
        {
            return states switch
            {
                nameof(UnitTests.UserTypeTest.Men) => true,
                nameof(UnitTests.UserTypeTest.Women) => true,
                nameof(UnitTests.UserTypeTest.None) => true,
                _ => throw new ArgumentOutOfRangeException(nameof(states), states, null)
            };
        }
        public static string ToDisplayFast(this UnitTests.UserTypeTest states)
        {
            return states switch
            {
                UnitTests.UserTypeTest.Men => "مرد",
                UnitTests.UserTypeTest.Women => "زن",
                UnitTests.UserTypeTest.None => "None",
                _ => throw new ArgumentOutOfRangeException(nameof(states), states, null)
            };
        }
        public static UnitTests.UserTypeTest[] GetValuesFast()
        {
            return new[]
            {
                UnitTests.UserTypeTest.Men,
                UnitTests.UserTypeTest.Women,
                UnitTests.UserTypeTest.None,
            };
        }
        public static string[] GetNamesFast()
        {
            return new[]
            {
                nameof(UnitTests.UserTypeTest.Men),
                nameof(UnitTests.UserTypeTest.Women),
                nameof(UnitTests.UserTypeTest.None),
            };
        }
        public static int GetLengthFast()
        {
            return 3;

        }
    }

You do not see this file inside the project. But you can use it.

Usage

var stringEnum = UserTypeTest.Men.ToStringFast(); //Men;

var isDefined = UserTypeTestEnumExtensions.IsDefinedFast(UserType.Men); //true;

var displayEnum = UserTypeTest.Men.ToDisplayFast(); //مرد

var names = UserTypeTestEnumExtensions.GetNamesFast(); //string[]

var values = UserTypeTestEnumExtensions.GetValuesFast(); //UserType[]

var length = UserTypeTestEnumExtensions.GetLengthFast(); //3

If you had trouble using UserTypeTestEnumExtensions and the IDE did not recognize it. This is an IDE problem and you need to restart the IDE once.

Benchmark

Benchmark

Contributing

Create an issue if you find a BUG or have a Suggestion or Question. If you want to develop this project :

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request

Give a Star! ⭐️

If you find this repository useful, please give it a star. Thanks!

License

Supernova.Enum.Generators is Copyright © 2022 Mohsen Rajabi under the MIT License.

enum.source.generator's People

Contributors

engrajabi avatar dependabot[bot] 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.