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, T 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, T obj, object value) { var c = ConvertValue(value, pi.PropertyType); pi.SetValue(obj, c, null); } public static object? GetValue(T obj, string name) where T : notnull { if (obj.GetType().GetTypeInfo().TryGetPropertyInfo(name, out var pi)) return pi.GetValue(obj, null); return null; } public static bool SetValue(T obj, string name, object value) where T : notnull { if (!obj.GetType().GetTypeInfo().TryGetPropertyInfo(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(CanReadOrWritePublic); } extension(PropertyInfo p) { private bool CanReadPublic() => p.CanRead && (p.GetMethod?.IsPublic ?? false); private bool CanWritePublic() => p.CanWrite && (p.SetMethod?.IsPublic ?? false); private bool CanReadOrWritePublic() => p.CanReadPublic() || p.CanWritePublic(); } 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); } extension(TypeInfo typeInfo) { public IEnumerable GetAllConstructors() => typeInfo.GetAll(ti => ti.DeclaredConstructors); public IEnumerable GetAllEvents() => typeInfo.GetAll(ti => ti.DeclaredEvents); public IEnumerable GetAllFields() => typeInfo.GetAll(ti => ti.DeclaredFields); public IEnumerable GetAllMembers() => typeInfo.GetAll(ti => ti.DeclaredMembers); public IEnumerable GetAllMethods() => typeInfo.GetAll(ti => ti.DeclaredMethods); public IEnumerable GetAllNestedTypes() => typeInfo.GetAll(ti => ti.DeclaredNestedTypes); public IEnumerable GetAllProperties() => typeInfo.GetAll(ti => ti.DeclaredProperties); } public static IEnumerable GetAllTypeInfo(this TypeInfo? typeInfo) { while (typeInfo is not 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(T obj, string name, [NotNullWhen(true)] out PropertyInfo? pi) where T : notnull { var type = obj.GetType(); return type.GetTypeInfo().TryGetPropertyInfo(name, out pi); } extension(TypeInfo typeInfo) { public bool TryGetPropertyInfo(string name, [NotNullWhen(true)] out PropertyInfo? pi) { foreach (var t in typeInfo.GetAllTypeInfo()) { pi = t.GetDeclaredProperty(name); if (pi is not null) return true; foreach (var i in t.ImplementedInterfaces) { pi = i.GetTypeInfo().GetDeclaredProperty(name); if (pi is not null) return true; } } pi = null; return false; } private IEnumerable GetAll(Func> accessor) { return typeInfo.GetAllTypeInfo().SelectMany(_ => accessor(typeInfo)); } } extension(Type type) { public Dictionary GetAllConstantsOfType() where T : unmanaged { 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 Dictionary GetAllPropertiesOfType(object obj) { var props = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); var result = new Dictionary(props.Length); var requestType = typeof(T); foreach (var pi in props) { if (!requestType.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; } } }