Skip to content

Add source generator #355

Open
Open
@aljaff94

Description

@aljaff94

Auto assign defined fields to name of field and incremental value start from 1
class must be defined as partial with Attribute [SmartEnumGenerator] and all fields must be static

example class with SmartEnumGenerator and no inheritance from SmartEnum

[SmartEnumGenerator]
public sealed partial class Permissions
{
    public static readonly Permissions Dashboard;
    public static readonly Permissions UserManagement;
}

generated code will be

namespace SmartEnum.SourceGenerator.UnitTests
{
    public sealed partial class Permissions  : SmartEnum <Permissions> 
    {
        public Permissions(string name, int value) : base(name, value) {}

        static Permissions()
        {
	   Dashboard = new Permissions(nameof(Dashboard), 1);
	   UserManagement = new Permissions(nameof(UserManagement), 2);
        }
    }
}

example class with SmartEnumGenerator and inheritanced from SmartEnum

namespace SmartEnum.SourceGenerator.UnitTests
{
    [SmartEnumGenerator]
    public sealed partial class Subscriptions : SmartEnum<Subscriptions>
    {
        public static readonly Subscriptions Free;
        public static readonly Subscriptions Sliver;
        public static readonly Subscriptions Gold;

        public Subscriptions(string name, int value) : base(name, value)
        {
        }
    }
}

generated code will be

namespace SmartEnum.SourceGenerator.UnitTests

{
    public sealed partial class Subscriptions  
    {
        static Subscriptions()
        {
	  Free = new Subscriptions(nameof(Free), 1);
	  Sliver = new Subscriptions(nameof(Sliver), 2);
	  Gold = new Subscriptions(nameof(Gold), 3);
        }
    }
}

Note: //in future we can define a custom attribute to ignore field
I'll do a pull request for this feature in separated package called SmartEnum.SourceGenerator

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions