Browse Source

ReflectionHelper add GetAllConstants method.

Get all the constant values in the specified type (including the base type).
pull/1059/head
maliming 7 years ago
parent
commit
7316870363
  1. 37
      framework/src/Volo.Abp.Core/Volo/Abp/Reflection/ReflectionHelper.cs
  2. 99
      framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/ReflectionHelper_Tests.cs

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

@ -165,5 +165,42 @@ namespace Volo.Abp.Reflection
property = currentType.GetProperty(properties.Last());
property.SetValue(obj, value);
}
/// <summary>
/// Get all the constant values in the specified type (including the base type).
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static string[] GetAllConstants(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;
if (currentDepth > maxRecursiveParameterValidationDepth)
{
return;
}
constants.AddRange(
type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
.Where(x => x.IsLiteral && !x.IsInitOnly)
.Select(x => x.GetValue(null).ToString())
);
var nestedTypes = type.GetNestedTypes(BindingFlags.Public);
foreach (var nestedType in nestedTypes)
{
GetAllConstantsRecursively(constants, nestedType, currentDepth + 1);
}
}
}
}

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

@ -1,7 +1,102 @@
namespace Volo.Abp.Reflection
using System.Linq;
using Shouldly;
using Xunit;
namespace Volo.Abp.Reflection
{
public class ReflectionHelper_Tests
{
//TODO: ...
[Fact]
public void GetAllConstants_Test()
{
var constants = ReflectionHelper.GetAllConstants(typeof(BaseRole));
constants.ShouldNotBeEmpty();
constants.Length.ShouldBe(1);
constants.ShouldContain(x => x == "DefaultBaseRoleName");
}
[Fact]
public void GetAllConstants_Inherit_Test()
{
var constants = ReflectionHelper.GetAllConstants(typeof(Roles));
constants.ShouldNotBeEmpty();
constants.Length.ShouldBe(2);
constants.ShouldContain(x => x == "DefaultBaseRoleName");
constants.ShouldContain(x => x == "DefaultRoleName");
}
[Fact]
public void GetAllConstants_NestedTypes_Test()
{
var constants = ReflectionHelper.GetAllConstants(typeof(IdentityPermissions));
constants.ShouldNotBeEmpty();
constants.Except(IdentityPermissions.GetAll()).Count().ShouldBe(0);
}
}
}
public class BaseRole
{
public const string BaseRoleName = "DefaultBaseRoleName";
}
public class Roles : BaseRole
{
public const string RoleName = "DefaultRoleName";
}
public static class IdentityPermissions
{
public const string GroupName = "AbpIdentity";
public static class Roles
{
public const string Default = GroupName + ".Roles";
public const string Create = Default + ".Create";
public const string Update = Default + ".Update";
public const string Delete = Default + ".Delete";
public const string ManagePermissions = Default + ".ManagePermissions";
}
public static class Users
{
public const string Default = GroupName + ".Users";
public const string Create = Default + ".Create";
public const string Update = Default + ".Update";
public const string Delete = Default + ".Delete";
public const string ManagePermissions = Default + ".ManagePermissions";
}
public static class UserLookup
{
public const string Default = GroupName + ".UserLookup";
}
public static string[] GetAll()
{
return new[]
{
GroupName,
Roles.Default,
Roles.Create,
Roles.Update,
Roles.Delete,
Roles.ManagePermissions,
Users.Default,
Users.Create,
Users.Update,
Users.Delete,
Users.ManagePermissions,
UserLookup.Default
};
}
}
}
Loading…
Cancel
Save