Enumerations support
Closed this issue · 1 comments
nzour commented
Example
public enum Repos
{
Github,
Gitlab
}
[Discriminator(property: "type")]
[DiscriminatorCase(when: Repos.Github, then: typeof(GithubRepository))]
[DiscriminatorCase(when: Repos.Gitlab, then: typeof(GitlabRepository))]
public abstract class IRepository
{
}
nzour commented
As far as I know, this is not possible, because we don't have base type for all enums.
So there are few solutions here:
-
We may allow to consume
object
and cast it tostring
public class DiscriminatorCaseAttribute : Attribute { - public string Key { get; } + public string? Key { get; } - public DiscriminatorCaseAttribute(string when, Type then) + public DiscriminatorCaseAttribute(object when, Type then) { - Key = when; + Key = when.ToString();
This way is not completely safe and also would not highlight cases for enums.
-
In your land you may extend attribute with your concrete enum.
public enum Repos { Github, Gitlab } public class DiscriminatorCaseForRepositoryAttribute : DiscriminatorCaseAttribute { public DiscriminatorCaseForRepositoryAttribute(Repos when, Type then) : base(when.ToString().Camelize(), then) { } }
And it would look like
[Discriminator(property: "type")] [DiscriminatorCaseForRepository(when: Repos.Github, then: typeof(GithubRepository))] [DiscriminatorCaseForRepository(when: Repos.Gitlab, then: typeof(GitlabRepository))] public interface IRepository { }
Also, keep in mind this nuance
base(when.ToString().Camelize(),
, specifiedwhen
argument would no bet transformed into other naming cases automatically