From c4372db9730da97893c4dd16dea4f03b9d703c7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Tue, 26 Aug 2025 14:22:42 +0300 Subject: [PATCH] Refactor property conversion logic to TypeHelper Moved the primitive type conversion logic from HasExtraPropertiesExtensions.GetProperty to a new TypeHelper.ChangeTypePrimitiveExtended method. This centralizes and simplifies type conversion, improving code maintainability and reuse. --- .../Volo/Abp/Reflection/TypeHelper.cs | 31 +++++++++++++++++++ .../Abp/Data/HasExtraPropertiesExtensions.cs | 31 ++----------------- 2 files changed, 34 insertions(+), 28 deletions(-) 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 8a67d20b31..0118fd9874 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs @@ -80,6 +80,37 @@ public static class TypeHelper return false; } + public static TProperty? ChangeTypePrimitiveExtended(object? value) + { + if (value == null) + { + return default; + } + + if (IsPrimitiveExtended(typeof(TProperty), includeEnums: true)) + { + var conversionType = typeof(TProperty); + if (IsNullable(conversionType)) + { + conversionType = conversionType.GetFirstGenericArgumentIfNullable(); + } + + if (conversionType == typeof(Guid)) + { + return (TProperty)TypeDescriptor.GetConverter(conversionType).ConvertFromInvariantString(value.ToString()!)!; + } + + if (conversionType.IsEnum) + { + return (TProperty)Enum.Parse(conversionType, value.ToString()!); + } + + return (TProperty)Convert.ChangeType(value, conversionType, CultureInfo.InvariantCulture); + } + + throw new AbpException("ChangeTypePrimitiveExtended does not support non-primitive types. Use non-generic GetProperty method and handle type casting manually."); + } + public static bool IsNullable(Type type) { return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>); diff --git a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/HasExtraPropertiesExtensions.cs b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/HasExtraPropertiesExtensions.cs index 75f5672a29..54cdc2bc94 100644 --- a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/HasExtraPropertiesExtensions.cs +++ b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/HasExtraPropertiesExtensions.cs @@ -26,34 +26,9 @@ public static class HasExtraPropertiesExtensions public static TProperty? GetProperty(this IHasExtraProperties source, string name, TProperty? defaultValue = default) { - var value = source.GetProperty(name); - if (value == null) - { - return defaultValue; - } - - if (TypeHelper.IsPrimitiveExtended(typeof(TProperty), includeEnums: true)) - { - var conversionType = typeof(TProperty); - if (TypeHelper.IsNullable(conversionType)) - { - conversionType = conversionType.GetFirstGenericArgumentIfNullable(); - } - - if (conversionType == typeof(Guid)) - { - return (TProperty)TypeDescriptor.GetConverter(conversionType).ConvertFromInvariantString(value.ToString()!)!; - } - - if (conversionType.IsEnum) - { - return (TProperty)Enum.Parse(conversionType, value.ToString()!); - } - - return (TProperty)Convert.ChangeType(value, conversionType, CultureInfo.InvariantCulture); - } - - throw new AbpException("GetProperty does not support non-primitive types. Use non-generic GetProperty method and handle type casting manually."); + return TypeHelper.ChangeTypePrimitiveExtended( + source.GetProperty(name, (object?) defaultValue) + ) ?? defaultValue; } public static TSource SetProperty(