Browse Source

Rename GetAllConstants to GetPublicConstantsRecursively.

pull/1059/head
maliming 7 years ago
parent
commit
f43209c6a4
  1. 39
      framework/src/Volo.Abp.Core/Volo/Abp/Reflection/ReflectionHelper.cs
  2. 12
      framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/ReflectionHelper_Tests.cs

39
framework/src/Volo.Abp.Core/Volo/Abp/Reflection/ReflectionHelper.cs

@ -172,35 +172,34 @@ namespace Volo.Abp.Reflection
/// </summary> /// </summary>
/// <param name="type"></param> /// <param name="type"></param>
/// <returns></returns> /// <returns></returns>
public static string[] GetAllConstants(Type type) public static string[] GetPublicConstantsRecursively(Type type)
{
var constants = new List<string>();
GetAllConstantsRecursively(constants, type, 1);
return constants.ToArray();
}
private static void GetAllConstantsRecursively(List<string> constants, Type type, int currentDepth)
{ {
const int maxRecursiveParameterValidationDepth = 8; const int maxRecursiveParameterValidationDepth = 8;
if (currentDepth > maxRecursiveParameterValidationDepth) var publicConstants = new List<string>();
void Recursively(List<string> constants, Type targetType, int currentDepth)
{ {
return; if (currentDepth > maxRecursiveParameterValidationDepth)
} {
return;
}
constants.AddRange( constants.AddRange(targetType.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
.Where(x => x.IsLiteral && !x.IsInitOnly) .Where(x => x.IsLiteral && !x.IsInitOnly)
.Select(x => x.GetValue(null).ToString()) .Select(x => x.GetValue(null).ToString()));
);
var nestedTypes = type.GetNestedTypes(BindingFlags.Public); var nestedTypes = targetType.GetNestedTypes(BindingFlags.Public);
foreach (var nestedType in nestedTypes) foreach (var nestedType in nestedTypes)
{ {
GetAllConstantsRecursively(constants, nestedType, currentDepth + 1); Recursively(constants, nestedType, currentDepth + 1);
}
} }
}
Recursively(publicConstants, type, 1);
return publicConstants.ToArray();
}
} }
} }

12
framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/ReflectionHelper_Tests.cs

@ -10,9 +10,9 @@ namespace Volo.Abp.Reflection
[Fact] [Fact]
public void GetAllConstants_Test() public void GetPublicConstantsRecursively_Test()
{ {
var constants = ReflectionHelper.GetAllConstants(typeof(BaseRole)); var constants = ReflectionHelper.GetPublicConstantsRecursively(typeof(BaseRole));
constants.ShouldNotBeEmpty(); constants.ShouldNotBeEmpty();
constants.Length.ShouldBe(1); constants.Length.ShouldBe(1);
@ -20,9 +20,9 @@ namespace Volo.Abp.Reflection
} }
[Fact] [Fact]
public void GetAllConstants_Inherit_Test() public void GetPublicConstantsRecursively_Inherit_Test()
{ {
var constants = ReflectionHelper.GetAllConstants(typeof(Roles)); var constants = ReflectionHelper.GetPublicConstantsRecursively(typeof(Roles));
constants.ShouldNotBeEmpty(); constants.ShouldNotBeEmpty();
constants.Length.ShouldBe(2); constants.Length.ShouldBe(2);
@ -32,9 +32,9 @@ namespace Volo.Abp.Reflection
[Fact] [Fact]
public void GetAllConstants_NestedTypes_Test() public void GetPublicConstantsRecursively_NestedTypes_Test()
{ {
var constants = ReflectionHelper.GetAllConstants(typeof(IdentityPermissions)); var constants = ReflectionHelper.GetPublicConstantsRecursively(typeof(IdentityPermissions));
constants.ShouldNotBeEmpty(); constants.ShouldNotBeEmpty();
constants.Except(IdentityPermissions.GetAll()).Count().ShouldBe(0); constants.Except(IdentityPermissions.GetAll()).Count().ShouldBe(0);

Loading…
Cancel
Save