using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Vanara.PInvoke
{
/// Gets a static field's name from its value and caches the list for faster lookups.
public class StaticFieldValueHash
{
private static Dictionary<(Type, Type), IDictionary> cache = new Dictionary<(Type, Type), IDictionary>();
/// Tries to get the name of a static field from it's value.
/// The type of the type.
/// The type of the field type.
/// The value for which to search.
/// On success, the name of the field.
/// if the value was found, otherwise .
public static bool TryGetFieldName(TFieldType value, out string fieldName)
{
var tt = (typeof(TType), typeof(TFieldType));
if (!cache.TryGetValue(tt, out var hash))
cache.Add(tt, hash = typeof(TType).GetFields(BindingFlags.Public | BindingFlags.Static).Where(fi => fi.FieldType == typeof(TFieldType)).ToDictionary(fi => fi.GetValue(null).GetHashCode(), fi => fi.Name));
return hash.TryGetValue(value.GetHashCode(), out fieldName);
}
}
}