From 793a600981b0448bd50b9488da4311df2a3f2ffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Wed, 18 Mar 2020 20:02:30 +0300 Subject: [PATCH] Fix TypeHelper.IsDictionary and add tests. --- .../Volo/Abp/Reflection/TypeHelper.cs | 14 +++++--- .../Volo/Abp/Reflection/TypeHelper_Tests.cs | 35 ++++++++++++++++++- 2 files changed, 44 insertions(+), 5 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 a9f21c5a0d..b62f21f124 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs @@ -84,11 +84,16 @@ namespace Volo.Abp.Reflection public static bool IsDictionary(Type type, out Type keyType, out Type valueType) { - var enumerableTypes = ReflectionHelper.GetImplementedGenericTypes(type, typeof(IDictionary<,>)); - if (enumerableTypes.Count == 1) + var dictionaryTypes = ReflectionHelper + .GetImplementedGenericTypes( + type, + typeof(IDictionary<,>) + ); + + if (dictionaryTypes.Count == 1) { - keyType = enumerableTypes[0].GenericTypeArguments[0]; - valueType = enumerableTypes[0].GenericTypeArguments[2]; + keyType = dictionaryTypes[0].GenericTypeArguments[0]; + valueType = dictionaryTypes[0].GenericTypeArguments[1]; return true; } @@ -101,6 +106,7 @@ namespace Volo.Abp.Reflection keyType = null; valueType = null; + return false; } diff --git a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/TypeHelper_Tests.cs b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/TypeHelper_Tests.cs index 0fc11addae..59378708b1 100644 --- a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/TypeHelper_Tests.cs +++ b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/TypeHelper_Tests.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Text; using Shouldly; using Xunit; @@ -16,5 +15,39 @@ namespace Volo.Abp.Reflection guidType.ShouldBe(typeof(Guid)); } + + [Fact] + public void IsDictionary() + { + //Dictionary + TypeHelper.IsDictionary( + typeof(Dictionary), + out var keyType, + out var valueType + ).ShouldBeTrue(); + keyType.ShouldBe(typeof(string)); + valueType.ShouldBe(typeof(int)); + + //MyDictionary + TypeHelper.IsDictionary( + typeof(MyDictionary), + out keyType, + out valueType + ).ShouldBeTrue(); + keyType.ShouldBe(typeof(bool)); + valueType.ShouldBe(typeof(TypeHelper_Tests)); + + //TypeHelper_Tests + TypeHelper.IsDictionary( + typeof(TypeHelper_Tests), + out keyType, + out valueType + ).ShouldBeFalse(); + } + + public class MyDictionary : Dictionary + { + + } } }