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 f9fdea3e50..06d2ee885b 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Collections.Immutable; using System.ComponentModel; using System.Globalization; using System.Linq; @@ -129,17 +130,22 @@ public static class TypeHelper public static bool IsDictionary(Type type, out Type? keyType, out Type? valueType) { - var dictionaryTypes = ReflectionHelper - .GetImplementedGenericTypes( - type, - typeof(IDictionary<,>) - ); + var knownDictionaryInterfaces = new Type[] + { + typeof(IDictionary<,>), + typeof(IReadOnlyDictionary<,>), + typeof(IImmutableDictionary<,>) + }; - if (dictionaryTypes.Count == 1) + foreach (var dictInterface in knownDictionaryInterfaces) { - keyType = dictionaryTypes[0].GenericTypeArguments[0]; - valueType = dictionaryTypes[0].GenericTypeArguments[1]; - return true; + var dictionaryTypes = ReflectionHelper.GetImplementedGenericTypes(type, dictInterface); + if (dictionaryTypes.Count == 1) + { + keyType = dictionaryTypes[0].GenericTypeArguments[0]; + valueType = dictionaryTypes[0].GenericTypeArguments[1]; + return true; + } } if (typeof(IDictionary).IsAssignableFrom(type)) @@ -151,7 +157,6 @@ public static class TypeHelper keyType = null; valueType = null; - return false; } diff --git a/framework/test/Volo.Abp.Http.Tests/Volo/Abp/Http/ApiTypeNameHelper_Tests.cs b/framework/test/Volo.Abp.Http.Tests/Volo/Abp/Http/ApiTypeNameHelper_Tests.cs index e202a95d47..78be2e174a 100644 --- a/framework/test/Volo.Abp.Http.Tests/Volo/Abp/Http/ApiTypeNameHelper_Tests.cs +++ b/framework/test/Volo.Abp.Http.Tests/Volo/Abp/Http/ApiTypeNameHelper_Tests.cs @@ -1,5 +1,6 @@ using System.Collections; using System.Collections.Generic; +using System.Collections.ObjectModel; using Shouldly; using Volo.Abp.Http.Modeling; using Volo.Abp.Reflection; @@ -25,6 +26,18 @@ public class ApiTypeNameHelper_Tests ApiTypeNameHelper.GetTypeName(typeof(CycleClass3)).ShouldBe($"[{TypeHelper.GetSimplifiedName(typeof(CycleClass4))}]"); } + + [Fact] + public void IsDictionary_Test() + { + ApiTypeNameHelper.GetSimpleTypeName(typeof(IDictionary)).ShouldBe("{string:number}"); + ApiTypeNameHelper.GetSimpleTypeName(typeof(Dictionary)).ShouldBe("{string:number}"); + ApiTypeNameHelper.GetSimpleTypeName(typeof(IReadOnlyDictionary)).ShouldBe("{string:number}"); + ApiTypeNameHelper.GetSimpleTypeName(typeof(ReadOnlyDictionary)).ShouldBe("{string:number}"); + ApiTypeNameHelper.GetSimpleTypeName(typeof(OrderedDictionary)).ShouldBe("{string:number}"); + ApiTypeNameHelper.GetSimpleTypeName(typeof(SortedDictionary)).ShouldBe("{string:number}"); + } + class CycleClass : IEnumerable { public IEnumerator GetEnumerator()