Skip to content

Why does TemplatesCiclo.List include values from all subclasses of Templates when using Ardalis.SmartEnum? #562

@art1544

Description

@art1544

I’m using [Ardalis.SmartEnum](https://github.com/ardalis/SmartEnum) to model a base enum class and two derived enums:

public abstract class Templates : SmartEnum<Templates> {}

public class TemplatesCiclo : Templates
{
    public static readonly TemplatesCiclo IdentificadorPedido = new(1, nameof(IdentificadorPedido));
    // … other members …
}

public class TemplatesNotaFiscal : Templates
{
    public static readonly TemplatesNotaFiscal ValorNota = new(9, nameof(ValorNota));
    // … other members …
}

When I iterate over TemplatesCiclo.List, I expected only the TemplatesCiclo instances, but I also get the TemplatesNotaFiscal values:

foreach (var tmpl in TemplatesCiclo.List)
{
    Console.WriteLine(tmpl.Name);
    // Prints both Ciclo members AND NotaFiscal members…
}
  • What is causing the child class’s List property to return every instance of the base class (including those from other subclasses)?
  • How can I restrict the list so that it only contains the instances defined in TemplatesCiclo?

Any insights into how SmartEnum registers and filters its static instances would be greatly appreciated!

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