Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to Dictionary with enum keys?

Tags:

c#

.net

In my solution i depend heavily on Dictionaries with an enum as a key. I find it is easy to understand and to read this construction.

One significant obstacle to the above is that it is not possible to serialize this. See Problems with Json Serialize Dictionary<Enum, Int32> for more info on this.

My question is:
Is there a equally readable and intuitive pattern for replacing the Dictionary<enunm,object> that is json serializable with the built in json serializer?

Today I have replaced a.Instance.QueryableFields[Fields.Title] with a.Instance.QueryableFields[ Fields.Title.ToString()] . Not very elegant, and it is opening up for errors.

like image 492
Fontanka16 Avatar asked Nov 25 '22 02:11

Fontanka16


1 Answers

When serializing it, just select the string value. It's not very neat, but it works.

a.Instance.QueryableFields.ToDictionary(x => x.Key.ToString(), x => x.Value)
like image 66
Ry- Avatar answered Dec 10 '22 11:12

Ry-