using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace LibHac.Tests { internal class ResultNameResolver : Result.IResultNameResolver { private Lazy> ResultNames { get; } = new Lazy>(GetResultNames); public bool TryResolveName(Result result, out string name) { return ResultNames.Value.TryGetValue(result, out name); } private static Dictionary GetResultNames() { var dict = new Dictionary(); Assembly assembly = typeof(Result).Assembly; foreach (TypeInfo type in assembly.DefinedTypes.Where(x => x.Name.Contains("Result"))) foreach (PropertyInfo property in type.DeclaredProperties .Where(x => x.PropertyType == typeof(Result.Base) && x.GetMethod?.IsStatic == true && x.SetMethod == null)) { object value = property.GetValue(null, null); if (value is null) continue; Result resultValue = ((Result.Base)value).Value; string name = $"{type.Name}{property.Name}"; dict[resultValue] = name; } return dict; } } }