using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Linq; using System.Reflection; namespace PKHeX.Core; /// /// Utility class for reflection. /// public static class ReflectUtil { /// /// Fetches the requested property from , and compares it to . /// /// Property to fetch /// Object to fetch property from /// Value to compare to /// Comparison result public static int CompareTo(this PropertyInfo pi, object obj, object value) { var v = pi.GetValue(obj, null); var c = ConvertValue(value, pi.PropertyType); if (v is null) return 0; if (c is IComparable c1 && v is IComparable c2) return c2.CompareTo(c1); return 0; } public static void SetValue(PropertyInfo pi, object obj, object value) { var c = ConvertValue(value, pi.PropertyType); pi.SetValue(obj, c, null); } public static object? GetValue(object obj, string name) { if (TryGetPropertyInfo(obj.GetType().GetTypeInfo(), name, out var pi)) return pi.GetValue(obj, null); return default; } public static bool SetValue(object obj, string name, object value) { if (!TryGetPropertyInfo(obj.GetType().GetTypeInfo(), name, out var pi)) return false; if (!pi.CanWrite) return false; pi.SetValue(obj, value); return true; } public static IEnumerable GetPropertiesStartWithPrefix(Type type, string prefix) { return type.GetTypeInfo().GetAllTypeInfo().SelectMany(GetAllProperties) .Where(p => p.Name.StartsWith(prefix, StringComparison.Ordinal)) .Select(p => p.Name) .Distinct() ; } public static IEnumerable GetPropertiesCanWritePublic(Type type) { return GetAllPropertyInfoCanWritePublic(type).Select(p => p.Name) .Distinct() ; } public static IEnumerable GetAllPropertyInfoCanWritePublic(Type type) { return type.GetTypeInfo().GetAllTypeInfo().SelectMany(GetAllProperties) .Where(CanWritePublic); } public static IEnumerable GetAllPropertyInfoPublic(Type type) { return type.GetTypeInfo().GetAllTypeInfo().SelectMany(GetAllProperties) .Where(p => p.CanReadPublic() || p.CanWritePublic()); } private static bool CanReadPublic(this PropertyInfo p) => p.CanRead && (p.GetMethod?.IsPublic ?? false); private static bool CanWritePublic(this PropertyInfo p) => p.CanWrite && (p.SetMethod?.IsPublic ?? false); public static IEnumerable GetPropertiesPublic(Type type) { return GetAllPropertyInfoPublic(type).Select(p => p.Name) .Distinct() ; } public static IEnumerable GetPropertiesCanWritePublicDeclared(Type type) { return type.GetTypeInfo().GetAllProperties() .Where(CanWritePublic) .Select(p => p.Name) .Distinct() ; } private static object? ConvertValue(object value, Type type) { if (type == typeof(DateOnly?)) // Used for PKM.MetDate and other similar properties { return DateOnly.TryParseExact(value.ToString(), "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateOnly dateValue) ? new DateOnly?(dateValue) : null; } if (type.IsEnum) { var str = value.ToString() ?? string.Empty; if (Enum.IsDefined(type, str) && int.TryParse(str, out var integer)) return Convert.ChangeType(integer, type); return Enum.Parse(type, str, true); } // Convert.ChangeType is suitable for most things return Convert.ChangeType(value, type); } public static IEnumerable GetAllConstructors(this TypeInfo typeInfo) => GetAll(typeInfo, ti => ti.DeclaredConstructors); public static IEnumerable GetAllEvents(this TypeInfo typeInfo) => GetAll(typeInfo, ti => ti.DeclaredEvents); public static IEnumerable GetAllFields(this TypeInfo typeInfo) => GetAll(typeInfo, ti => ti.DeclaredFields); public static IEnumerable GetAllMembers(this TypeInfo typeInfo) => GetAll(typeInfo, ti => ti.DeclaredMembers); public static IEnumerable GetAllMethods(this TypeInfo typeInfo) => GetAll(typeInfo, ti => ti.DeclaredMethods); public static IEnumerable GetAllNestedTypes(this TypeInfo typeInfo) => GetAll(typeInfo, ti => ti.DeclaredNestedTypes); public static IEnumerable GetAllProperties(this TypeInfo typeInfo) => GetAll(typeInfo, ti => ti.DeclaredProperties); public static IEnumerable GetAllTypeInfo(this TypeInfo? typeInfo) { while (typeInfo != null) { yield return typeInfo; typeInfo = typeInfo.BaseType?.GetTypeInfo(); } } /// /// Checks if the has the requested property . /// /// Object to check for property existence. /// Name of the property. /// Reference to the property info for the object, if it exists. /// True if it has property, and false if it does not have property. is null when returning false. public static bool HasProperty(object obj, string name, [NotNullWhen(true)] out PropertyInfo? pi) { var type = obj.GetType(); return TryGetPropertyInfo(type.GetTypeInfo(), name, out pi); } public static bool TryGetPropertyInfo(this TypeInfo typeInfo, string name, [NotNullWhen(true)] out PropertyInfo? pi) { foreach (var t in typeInfo.GetAllTypeInfo()) { pi = t.GetDeclaredProperty(name); if (pi != null) return true; foreach (var i in t.ImplementedInterfaces) { pi = i.GetTypeInfo().GetDeclaredProperty(name); if (pi != null) return true; } } pi = null; return false; } private static IEnumerable GetAll(this TypeInfo typeInfo, Func> accessor) { return GetAllTypeInfo(typeInfo).SelectMany(_ => accessor(typeInfo)); } public static Dictionary GetAllConstantsOfType(this Type type) where T : struct { var fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.FlattenHierarchy); var consts = fields.Where(fi => fi is { IsLiteral: true, IsInitOnly: false } && fi.FieldType == typeof(T)); return consts.ToDictionary(z => (T)(z.GetRawConstantValue() ?? throw new NullReferenceException(nameof(z.Name))), z => z.Name); } public static Dictionary GetAllPropertiesOfType(this Type type, object obj) where T : class { var props = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); var result = new Dictionary(props.Length); foreach (var pi in props) { if (!typeof(T).IsAssignableFrom(pi.PropertyType)) continue; var name = pi.Name; var value = pi.GetValue(obj); if (value is not T t) continue; result.TryAdd(name, t); } return result; } }