2023-01-22 04:02:33 +00:00
|
|
|
using System;
|
2016-07-16 23:13:33 +00:00
|
|
|
using System.Collections.Generic;
|
2021-01-17 08:05:07 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2016-08-11 02:29:21 +00:00
|
|
|
using System.Globalization;
|
2016-07-16 23:13:33 +00:00
|
|
|
using System.Linq;
|
2016-07-16 17:00:50 +00:00
|
|
|
using System.Reflection;
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
2023-04-01 23:52:12 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Utility class for reflection.
|
|
|
|
/// </summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
public static class ReflectUtil
|
2016-07-16 17:00:50 +00:00
|
|
|
{
|
2023-04-01 23:52:12 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Fetches the requested property from <see cref="obj"/>, and compares it to <see cref="value"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="pi">Property to fetch</param>
|
|
|
|
/// <param name="obj">Object to fetch property from</param>
|
|
|
|
/// <param name="value">Value to compare to</param>
|
|
|
|
/// <returns>Comparison result</returns>
|
2023-01-22 04:02:33 +00:00
|
|
|
public static int CompareTo(this PropertyInfo pi, object obj, object value)
|
2016-07-16 17:00:50 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var v = pi.GetValue(obj, null);
|
|
|
|
var c = ConvertValue(value, pi.PropertyType);
|
2023-01-22 04:02:33 +00:00
|
|
|
if (v is null)
|
|
|
|
return 0;
|
|
|
|
if (c is IComparable c1 && v is IComparable c2)
|
|
|
|
return c2.CompareTo(c1);
|
|
|
|
return 0;
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static void SetValue(PropertyInfo pi, object obj, object value)
|
|
|
|
{
|
|
|
|
var c = ConvertValue(value, pi.PropertyType);
|
|
|
|
pi.SetValue(obj, c, null);
|
|
|
|
}
|
2017-01-08 07:54:09 +00:00
|
|
|
|
2023-09-02 22:53:51 +00:00
|
|
|
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;
|
|
|
|
}
|
2017-03-22 04:40:33 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static IEnumerable<string> GetPropertiesStartWithPrefix(Type type, string prefix)
|
|
|
|
{
|
|
|
|
return type.GetTypeInfo().GetAllTypeInfo().SelectMany(GetAllProperties)
|
2017-01-03 06:17:47 +00:00
|
|
|
.Where(p => p.Name.StartsWith(prefix, StringComparison.Ordinal))
|
2018-05-20 14:46:59 +00:00
|
|
|
.Select(p => p.Name)
|
|
|
|
.Distinct()
|
2022-06-18 18:04:24 +00:00
|
|
|
;
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static IEnumerable<string> GetPropertiesCanWritePublic(Type type)
|
|
|
|
{
|
|
|
|
return GetAllPropertyInfoCanWritePublic(type).Select(p => p.Name)
|
|
|
|
.Distinct()
|
|
|
|
;
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static IEnumerable<PropertyInfo> GetAllPropertyInfoCanWritePublic(Type type)
|
|
|
|
{
|
|
|
|
return type.GetTypeInfo().GetAllTypeInfo().SelectMany(GetAllProperties)
|
2023-01-22 04:02:33 +00:00
|
|
|
.Where(CanWritePublic);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static IEnumerable<PropertyInfo> GetAllPropertyInfoPublic(Type type)
|
|
|
|
{
|
|
|
|
return type.GetTypeInfo().GetAllTypeInfo().SelectMany(GetAllProperties)
|
2023-01-22 04:02:33 +00:00
|
|
|
.Where(p => p.CanReadPublic() || p.CanWritePublic());
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
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);
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static IEnumerable<string> GetPropertiesPublic(Type type)
|
|
|
|
{
|
|
|
|
return GetAllPropertyInfoPublic(type).Select(p => p.Name)
|
|
|
|
.Distinct()
|
|
|
|
;
|
|
|
|
}
|
2018-05-18 05:43:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static IEnumerable<string> GetPropertiesCanWritePublicDeclared(Type type)
|
|
|
|
{
|
|
|
|
return type.GetTypeInfo().GetAllProperties()
|
2023-01-22 04:02:33 +00:00
|
|
|
.Where(CanWritePublic)
|
2018-05-20 14:46:59 +00:00
|
|
|
.Select(p => p.Name)
|
2022-06-18 18:04:24 +00:00
|
|
|
.Distinct()
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static object? ConvertValue(object value, Type type)
|
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
if (type == typeof(DateOnly?)) // Used for PKM.MetDate and other similar properties
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
return DateOnly.TryParseExact(value.ToString(), "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateOnly dateValue)
|
|
|
|
? new DateOnly?(dateValue)
|
2022-06-18 18:04:24 +00:00
|
|
|
: null;
|
2016-07-18 00:06:50 +00:00
|
|
|
}
|
2016-08-10 18:43:00 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (type.IsEnum)
|
2016-08-10 18:43:00 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
var str = value.ToString() ?? string.Empty;
|
2022-06-18 18:04:24 +00:00
|
|
|
if (int.TryParse(str, out var integer))
|
|
|
|
return Convert.ChangeType(integer, type);
|
|
|
|
return Enum.Parse(type, str, true);
|
2016-08-10 18:43:00 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
// Convert.ChangeType is suitable for most things
|
|
|
|
return Convert.ChangeType(value, type);
|
|
|
|
}
|
2018-05-18 05:43:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static IEnumerable<ConstructorInfo> GetAllConstructors(this TypeInfo typeInfo)
|
|
|
|
=> GetAll(typeInfo, ti => ti.DeclaredConstructors);
|
2018-05-18 05:43:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static IEnumerable<EventInfo> GetAllEvents(this TypeInfo typeInfo)
|
|
|
|
=> GetAll(typeInfo, ti => ti.DeclaredEvents);
|
2018-05-18 05:43:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static IEnumerable<FieldInfo> GetAllFields(this TypeInfo typeInfo)
|
|
|
|
=> GetAll(typeInfo, ti => ti.DeclaredFields);
|
2018-05-18 05:43:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static IEnumerable<MemberInfo> GetAllMembers(this TypeInfo typeInfo)
|
|
|
|
=> GetAll(typeInfo, ti => ti.DeclaredMembers);
|
2018-05-18 05:43:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static IEnumerable<MethodInfo> GetAllMethods(this TypeInfo typeInfo)
|
|
|
|
=> GetAll(typeInfo, ti => ti.DeclaredMethods);
|
2018-05-18 05:43:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static IEnumerable<TypeInfo> GetAllNestedTypes(this TypeInfo typeInfo)
|
|
|
|
=> GetAll(typeInfo, ti => ti.DeclaredNestedTypes);
|
2018-05-18 05:43:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static IEnumerable<PropertyInfo> GetAllProperties(this TypeInfo typeInfo)
|
|
|
|
=> GetAll(typeInfo, ti => ti.DeclaredProperties);
|
2018-05-18 05:43:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static IEnumerable<TypeInfo> GetAllTypeInfo(this TypeInfo? typeInfo)
|
|
|
|
{
|
|
|
|
while (typeInfo != null)
|
2018-05-18 05:43:07 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
yield return typeInfo;
|
|
|
|
typeInfo = typeInfo.BaseType?.GetTypeInfo();
|
2018-05-18 05:43:07 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if the <see cref="obj"/> has the requested property <see cref="name"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="obj">Object to check for property existence.</param>
|
|
|
|
/// <param name="name">Name of the property.</param>
|
|
|
|
/// <param name="pi">Reference to the property info for the object, if it exists.</param>
|
|
|
|
/// <returns>True if has property, and false if does not have property. <see cref="pi"/> is null when returning false.</returns>
|
2023-09-02 22:53:51 +00:00
|
|
|
public static bool HasProperty(object obj, string name, [NotNullWhen(true)] out PropertyInfo? pi)
|
|
|
|
{
|
|
|
|
var type = obj.GetType();
|
|
|
|
return TryGetPropertyInfo(type.GetTypeInfo(), name, out pi);
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2023-09-02 22:53:51 +00:00
|
|
|
public static bool TryGetPropertyInfo(this TypeInfo typeInfo, string name, [NotNullWhen(true)] out PropertyInfo? pi)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-09-02 22:53:51 +00:00
|
|
|
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;
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static IEnumerable<T> GetAll<T>(this TypeInfo typeInfo, Func<TypeInfo, IEnumerable<T>> accessor)
|
|
|
|
{
|
|
|
|
return GetAllTypeInfo(typeInfo).SelectMany(_ => accessor(typeInfo));
|
|
|
|
}
|
2020-01-23 01:06:23 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static Dictionary<T, string> GetAllConstantsOfType<T>(this Type type) where T : struct
|
|
|
|
{
|
|
|
|
var fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.FlattenHierarchy);
|
2023-01-22 04:02:33 +00:00
|
|
|
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);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2020-01-24 04:11:39 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static Dictionary<T, string> GetAllPropertiesOfType<T>(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));
|
2023-01-22 04:02:33 +00:00
|
|
|
return ofType.ToDictionary(x => (T)(x.GetValue(obj) ?? throw new NullReferenceException(nameof(x.Name))), z => z.Name);
|
2016-07-16 17:00:50 +00:00
|
|
|
}
|
|
|
|
}
|