LibHac/tests/LibHac.Tests/ResultNameResolver.cs

39 lines
1.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
2021-11-14 20:08:57 +01:00
namespace LibHac.Tests;
internal class ResultNameResolver : Result.IResultNameResolver
{
2021-11-14 20:08:57 +01:00
private Lazy<Dictionary<Result, string>> ResultNames { get; } = new Lazy<Dictionary<Result, string>>(GetResultNames);
2021-11-14 20:08:57 +01:00
public bool TryResolveName(Result result, out string name)
{
return ResultNames.Value.TryGetValue(result, out name);
}
2021-11-14 20:08:57 +01:00
private static Dictionary<Result, string> GetResultNames()
{
var dict = new Dictionary<Result, string>();
2021-11-14 20:08:57 +01:00
Assembly assembly = typeof(Result).Assembly;
2021-11-14 20:08:57 +01:00
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;
2021-11-14 20:08:57 +01:00
Result resultValue = ((Result.Base)value).Value;
string name = $"{type.Name}{property.Name}";
2021-11-14 20:08:57 +01:00
dict[resultValue] = name;
}
2021-11-14 20:08:57 +01:00
return dict;
}
}