Skip to content

Instantly share code, notes, and snippets.

@616b2f
Last active June 6, 2019 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 616b2f/4bc0e90005318eeac103bf171dcbe8d2 to your computer and use it in GitHub Desktop.
Save 616b2f/4bc0e90005318eeac103bf171dcbe8d2 to your computer and use it in GitHub Desktop.
Generic StringEnumConverter<T> for C# version < 7.3
using System;
using System.Reflection;
using Newtonsoft.Json.Converters;
public class StringEnumConverter<TEnum> : StringEnumConverter where TEnum : struct, IConvertible
{
public override bool CanConvert(Type objectType)
{
Type t = Nullable.GetUnderlyingType(objectType) ?? objectType;
return t.IsEnum && t == typeof(TEnum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment