Compare commits

..

No commits in common. "ebba26e0bcb902de02a36eebeba83df8172b794b" and "c393ea325c9a5465618a5f3e745a1f9af76dd845" have entirely different histories.

1 changed files with 0 additions and 47 deletions

View File

@ -1,47 +0,0 @@
using System.Reflection;
namespace AyCode.Core.Helpers;
public sealed class ConstHelper
{
private static readonly Dictionary<string, Dictionary<int, string>?> ObjectsByNamesByValue = new();
public static string NameByValue<T>(int value) where T : class
=> GetNameByValue(GetNamesByType(typeof(T).Name) ?? FillNames<T>(), value);
public static string NameByValue(string typeName, int value)
=> GetNameByValue(GetNamesByType(typeName), value);
private static string GetNameByValue(IReadOnlyDictionary<int, string>? namesByValue, int value)
=> namesByValue != null && namesByValue.TryGetValue(value, out var fieldName) ? $"[{fieldName}_{value}]" : $"..._{value}]";
private static Dictionary<int, string>? GetNamesByType(string typeName)
{
lock (ObjectsByNamesByValue)
{
if (ObjectsByNamesByValue.TryGetValue(typeName, out var namesByValue) && namesByValue!.Count > 0)
return namesByValue;
}
return null;
}
private static Dictionary<int, string> FillNames<T>() where T : class
{
var objectType = typeof(T);
var namesByValue = new Dictionary<int, string>();
lock (ObjectsByNamesByValue)
{
foreach (var fld in objectType.GetFields().Where(x => (x.Attributes & FieldAttributes.Literal) != 0 && x.FieldType == typeof(int)))
{
namesByValue[(int)fld.GetValue(null)!] = fld.Name;
}
ObjectsByNamesByValue[objectType.Name] = namesByValue;
}
return namesByValue;
}
}