Browse Source

Fix TypeHelper.IsDictionary and add tests.

pull/3193/head
Halil İbrahim Kalkan 6 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)
{
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;
}

35
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<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