Created
November 17, 2017 20:31
-
-
Save kmorcinek/7e872d1fccc1771d9ef39cb83332b25a to your computer and use it in GitHub Desktop.
EnumGuard for better exception throwing when code have not handled enum values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using JetBrains.Annotations; | |
public class EnumGuard | |
{ | |
[Pure] | |
public static ArgumentOutOfRangeException CreateMissingEnumException<T>(string paramName, T value) | |
where T : struct | |
{ | |
return new ArgumentOutOfRangeException( | |
paramName, | |
value, | |
$"Enum '{typeof(T).Name}' has invalid value '{value}'"); | |
} | |
[Pure] | |
public static ArgumentOutOfRangeException CreateMissingEnumException<T>(string paramName, T? value) | |
where T : struct | |
{ | |
return new ArgumentOutOfRangeException( | |
paramName, | |
value, | |
$"Nullable enum '{typeof(T).Name}' has invalid value '{value}'"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment