-1

I have a number of lists that I want to store as statics, so they are explicitly named.

Gender, MaritalStatus and ResidentalStatus are all enum types.

static readonly Lazy<IEnumerable<EnumDto>> _gender = GetLazyOptions<Gender>();
static readonly Lazy<IEnumerable<EnumDto>> _maritalStatus = GetLazyOptions<MaritalStatus>();
static readonly Lazy<IEnumerable<EnumDto>> _residentalStatus = GetLazyOptions<ResidentialStatus>();

I need to be able to access the appropriate field using the generic method - something like the ones below:

public static IEnumerable<EnumDto> GetOption1<TEnum>() where TEnum : Enum
{
}

public static IEnumerable<EnumDto> GetOptions2<TEnum>(this TEnum @enum) where TEnum : Enum
{
}

The idea is then to then use a switch statement to find the right field (from the Enum type), but as yet I haven't been able to get it to work for me syntactically:

switch(typeof(@enum))
{
    case Gender: return _gender;
}

I've already checked out this thread, but with no success:

c# 7.0: switch on System.Type

Any advice appreciated.

GSerg
  • 76,472
  • 17
  • 159
  • 346
John Ohara
  • 2,821
  • 3
  • 28
  • 54
  • 1
    Did https://stackoverflow.com/a/43080709/34092 work for you? – mjwills Jun 10 '19 at 09:29
  • enum's are inherently ints (they are basically labels assigned an int value). – Steve Todd Jun 10 '19 at 09:33
  • Any reason you can't .ToString() them and switch on the string value (I'm assuming you want to switch on a generic enum because you have multiple enums, with the same name or names repeated among them?) – Steve Todd Jun 10 '19 at 09:36
  • 1
    It would be awesome if you could provide a [mcve]. – mjwills Jun 10 '19 at 09:38
  • It's generally a bad idea to store things like this in a `static` field. Why are you doing this? – Enigmativity Jun 10 '19 at 09:41
  • @Enigmativity Because there are only 10 lists, they don't change, and otherwise the lists are created using reflection every time one of a 1000 daily users access them. – John Ohara Jun 10 '19 at 09:44
  • @SteveTodd - I think you've got the wrong idea Steve, I'm not switching on enum values, just the generic enum type. Enum is the underlying type and I'm wanting to switch on Gender or MaritalStatus etc using the value as the 'switch'. – John Ohara Jun 10 '19 at 09:48
  • @JohnOhara - It doesn't seem to be a good idea still. – Enigmativity Jun 10 '19 at 09:53

2 Answers2

2

Perhaps try doing something like this:

public class Dtos
{
    private Dictionary<Type, Func<IEnumerable<EnumDto>>> _lists
        = new Dictionary<Type, Func<IEnumerable<EnumDto>>>();

    public void Add<T>(Func<IEnumerable<EnumDto>> factory)
    {
        _lists[typeof(T)] = factory;
    }

    public IEnumerable<EnumDto> Get<T>()
    {
        return _lists[typeof(T)]();
    }
}

Then you can configure the Dtos like this:

var dtos = new Dtos();

dtos.Add<Gender>(() => _gender.Value);
dtos.Add<MaritalStatus>(() => _maritalStatus.Value);
dtos.Add<ResidentialStatus>(() => _residentalStatus.Value);

And then retrieving them is simple:

IEnumerable<EnumDto> genderDtos = dtos.Get<Gender>();
IEnumerable<EnumDto> maritalStatusDtos = dtos.Get<MaritalStatus>();
IEnumerable<EnumDto> residentialStatusDtos = dtos.Get<ResidentialStatus>();
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • Thanks for your efforts Enigmativity, I'll see if it fits in with the rest of the setup. – John Ohara Jun 10 '19 at 09:59
  • @JohnOhara - It should only matter if it answers the question. If it doesn't fit in with your setup perhaps you should think how you would change your question to factor in what fits? – Enigmativity Jun 10 '19 at 10:20
  • I have an answer to the original question, which I'll post, but I've implemented a much cleaner version based around your solution - so marked you up – John Ohara Jun 10 '19 at 11:53
1

Found the answer thanks to mjwills here

    public static IEnumerable<EnumDto> GetOptions<TEnum>() where TEnum : Enum
    {
        var type = typeof(TEnum);
        switch (type)
        {
            case Type _ when type == typeof(Gender):
                return _gender.Value;
            default:
                return _maritalStatus.Value;
        }
    }

However, after looking at the answer from Enigmativity, I decided to implement a solution based around his much cleaner concept.

John Ohara
  • 2,821
  • 3
  • 28
  • 54