ardalis / ardalis/SmartEnum

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

Open
#562 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
2.4k
Forks
184
PR merge metrics
No merged PRs in 30d

Description

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

```csharp
public abstract class Templates : SmartEnum { … }

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:

```csharp
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!

Contributor guide

Open the contributing guide

Research direction

Start by reading the SmartEnum implementation of TemplatesCiclo.List and the static-instance registration and filtering behavior it uses. Reproduce the iteration with the two subclasses shown, then determine whether the completed behavior should return only TemplatesCiclo instances while preserving the base-class behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.