using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Reflection; namespace PKHeX.Core { public static class ReflectUtil { public static bool IsValueEqual(this PropertyInfo pi, object obj, object value) { var v = pi.GetValue(obj, null); var c = ConvertValue(value, pi.PropertyType); return v.Equals(c); } 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) => GetPropertyInfo(obj.GetType().GetTypeInfo(), name)?.GetValue(obj); public static void SetValue(object obj, string name, object value) => GetPropertyInfo(obj.GetType().GetTypeInfo(), name)?.SetValue(obj, value, null); public static object GetValue(Type t, string propertyName) => t.GetTypeInfo().GetDeclaredProperty(propertyName).GetValue(null); public static void SetValue(Type t, string propertyName, object value) => t.GetTypeInfo().GetDeclaredProperty(propertyName).SetValue(null, value); 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(p => p.CanWrite && p.SetMethod.IsPublic); } public static IEnumerable GetAllPropertyInfoPublic(Type type) { return type.GetTypeInfo().GetAllTypeInfo().SelectMany(GetAllProperties) .Where(p => (p.CanRead && p.GetMethod.IsPublic) || (p.CanWrite && p.SetMethod.IsPublic)); } 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(p => p.CanWrite && p.SetMethod.IsPublic) .Select(p => p.Name) .Distinct() ; } private static object? ConvertValue(object value, Type type) { if (type == typeof(DateTime?)) // Used for PKM.MetDate and other similar properties { return DateTime.TryParseExact(value.ToString(), "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateValue) ? new DateTime?(dateValue) : null; } // 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 has property, and false if does not have property. is null when returning false. public static bool HasProperty(object obj, string name, out PropertyInfo? pi) => (pi = GetPropertyInfo(obj.GetType().GetTypeInfo(), name)) != null; public static PropertyInfo? GetPropertyInfo(this TypeInfo typeInfo, string name) { return typeInfo.GetAllTypeInfo().Select(t => t.GetDeclaredProperty(name)).FirstOrDefault(pi => pi != null); } 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.IsLiteral && !fi.IsInitOnly && fi.FieldType == typeof(T)); return consts.ToDictionary(x => (T)x.GetRawConstantValue(), 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 ofType = props.Where(fi => typeof(T).IsAssignableFrom(fi.PropertyType)); return ofType.ToDictionary(x => (T)x.GetValue(obj), z => z.Name); } } }