Browse Source

Fix TypeHelper.IsDictionary and add tests.

pull/3193/head
Halil İbrahim Kalkan 7 years ago
parent
commit
793a600981
  1. 14
      framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs
  2. 35
      framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/TypeHelper_Tests.cs

14
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) public static bool IsDictionary(Type type, out Type keyType, out Type valueType)
{ {
var enumerableTypes = ReflectionHelper.GetImplementedGenericTypes(type, typeof(IDictionary<,>)); var dictionaryTypes = ReflectionHelper
if (enumerableTypes.Count == 1) .GetImplementedGenericTypes(
type,
typeof(IDictionary<,>)
);
if (dictionaryTypes.Count == 1)
{ {
keyType = enumerableTypes[0].GenericTypeArguments[0]; keyType = dictionaryTypes[0].GenericTypeArguments[0];
valueType = enumerableTypes[0].GenericTypeArguments[2]; valueType = dictionaryTypes[0].GenericTypeArguments[1];
return true; return true;
} }
@ -101,6 +106,7 @@ namespace Volo.Abp.Reflection
keyType = null; keyType = null;
valueType = null; valueType = null;
return false; return false;
} }

35
framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/TypeHelper_Tests.cs

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text;
using Shouldly; using Shouldly;
using Xunit; using Xunit;
@ -16,5 +15,39 @@ namespace Volo.Abp.Reflection
guidType.ShouldBe(typeof(Guid)); guidType.ShouldBe(typeof(Guid));
} }
[Fact]
public void IsDictionary()
{
//Dictionary<string, int>
TypeHelper.IsDictionary(
typeof(Dictionary<string, int>),
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<bool, TypeHelper_Tests>
{
}
} }
} }

Loading…
Cancel
Save