From 362e5eb51dd574b5d2548363daef1a589ae12132 Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 19 Dec 2024 17:05:38 +0800 Subject: [PATCH] Return `EnumValues` for `nullable enum`. --- .../CachedObjectExtensionsDtoService.cs | 17 ++++++++++++++--- .../Volo/Abp/Reflection/TypeHelper.cs | 8 ++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ObjectExtending/CachedObjectExtensionsDtoService.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ObjectExtending/CachedObjectExtensionsDtoService.cs index 10900f6ee4..b10b9d7a80 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ObjectExtending/CachedObjectExtensionsDtoService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ObjectExtending/CachedObjectExtensionsDtoService.cs @@ -242,7 +242,7 @@ public class CachedObjectExtensionsDtoService : ICachedObjectExtensionsDtoServic e => e.GetProperties() ) ) - .Where(p => p.Type.IsEnum) + .Where(p => p.Type.IsEnum || TypeHelper.IsNullableEnum(p.Type)) .ToList(); foreach (var enumProperty in enumProperties) @@ -260,12 +260,23 @@ public class CachedObjectExtensionsDtoService : ICachedObjectExtensionsDtoServic LocalizationResource = enumProperty.GetLocalizationResourceNameOrNull() }; - foreach (var enumValue in enumProperty.Type.GetEnumValues()) + var enumType = enumProperty.Type.IsEnum + ? enumProperty.Type + : TypeHelper.IsNullableEnum(enumProperty.Type) + ? Nullable.GetUnderlyingType(enumProperty.Type) + : null; + + if (enumType == null) + { + return extensionEnumDto; + } + + foreach (var enumValue in enumType.GetEnumValues()) { extensionEnumDto.Fields.Add( new ExtensionEnumFieldDto { - Name = enumProperty.Type.GetEnumName(enumValue)!, + Name = enumType.GetEnumName(enumValue)!, Value = enumValue } ); diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs index 1efa6c4f29..61ade9f6cc 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs @@ -84,6 +84,14 @@ public static class TypeHelper return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>); } + public static bool IsNullableEnum(Type type) + { + return type.IsGenericType && + type.GetGenericTypeDefinition() == typeof(Nullable<>) && + type.GenericTypeArguments.Length == 1 && + type.GenericTypeArguments[0].IsEnum; + } + public static Type GetFirstGenericArgumentIfNullable(this Type t) { if (t.GetGenericArguments().Length > 0 && t.GetGenericTypeDefinition() == typeof(Nullable<>))