Handle nullable enum default values (dotnet/extensions#531)

\n\nCommit migrated from 33f839d585
This commit is contained in:
Pavel Krymets 2018-11-16 13:57:43 -08:00 committed by GitHub
parent 4541e05c57
commit ebb1165b14
1 changed files with 15 additions and 0 deletions

View File

@ -8,6 +8,8 @@ namespace Microsoft.Extensions.Internal
{
internal class ParameterDefaultValue
{
private static readonly Type _nullable = typeof(Nullable<>);
public static bool TryGetDefaultValue(ParameterInfo parameter, out object defaultValue)
{
bool hasDefaultValue;
@ -39,6 +41,19 @@ namespace Microsoft.Extensions.Internal
{
defaultValue = Activator.CreateInstance(parameter.ParameterType);
}
// Handle nullable enums
if (defaultValue != null &&
parameter.ParameterType.IsGenericType &&
parameter.ParameterType.GetGenericTypeDefinition() == _nullable
)
{
var underlyingType = Nullable.GetUnderlyingType(parameter.ParameterType);
if (underlyingType != null && underlyingType.IsEnum)
{
defaultValue = Enum.ToObject(underlyingType, defaultValue);
}
}
}
return hasDefaultValue;