2016-07-16 17:00:50 +00:00
|
|
|
|
using System;
|
2016-07-16 23:13:33 +00:00
|
|
|
|
using System.Collections.Generic;
|
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;
|
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
namespace PKHeX.Core
|
2016-07-16 17:00:50 +00:00
|
|
|
|
{
|
2017-03-03 05:00:41 +00:00
|
|
|
|
public static class ReflectUtil
|
2016-07-16 17:00:50 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static bool IsValueEqual(object obj, string propertyName, object value)
|
2016-07-16 17:00:50 +00:00
|
|
|
|
{
|
2017-05-12 04:34:18 +00:00
|
|
|
|
PropertyInfo pi = obj.GetType().GetTypeInfo().GetDeclaredProperty(propertyName);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
if (pi == null)
|
|
|
|
|
return false;
|
2016-07-16 17:00:50 +00:00
|
|
|
|
var v = pi.GetValue(obj, null);
|
2016-08-10 18:43:00 +00:00
|
|
|
|
var c = ConvertValue(value, pi.PropertyType);
|
2016-07-16 17:00:50 +00:00
|
|
|
|
return v.Equals(c);
|
|
|
|
|
}
|
2017-01-08 07:54:09 +00:00
|
|
|
|
public static void SetValue(object obj, string propertyName, object value)
|
2016-07-16 17:00:50 +00:00
|
|
|
|
{
|
2017-05-12 04:34:18 +00:00
|
|
|
|
PropertyInfo pi = obj.GetType().GetTypeInfo().GetDeclaredProperty(propertyName);
|
2016-08-10 18:43:00 +00:00
|
|
|
|
pi.SetValue(obj, ConvertValue(value, pi.PropertyType), null);
|
2016-07-16 17:00:50 +00:00
|
|
|
|
}
|
2017-01-08 07:54:09 +00:00
|
|
|
|
|
|
|
|
|
public static object GetValue(object obj, string propertyName)
|
2016-07-16 23:13:33 +00:00
|
|
|
|
{
|
2017-05-12 04:34:18 +00:00
|
|
|
|
PropertyInfo pi = obj.GetType().GetTypeInfo().GetDeclaredProperty(propertyName);
|
2016-07-16 23:13:33 +00:00
|
|
|
|
return pi.GetValue(obj, null);
|
|
|
|
|
}
|
2017-01-08 07:54:09 +00:00
|
|
|
|
|
2017-05-12 04:34:18 +00:00
|
|
|
|
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);
|
2017-03-22 04:40:33 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static IEnumerable<string> GetPropertiesStartWithPrefix(Type type, string prefix)
|
2016-07-16 23:13:33 +00:00
|
|
|
|
{
|
2017-05-12 04:34:18 +00:00
|
|
|
|
return type.GetTypeInfo().DeclaredProperties
|
2017-01-03 06:17:47 +00:00
|
|
|
|
.Where(p => p.Name.StartsWith(prefix, StringComparison.Ordinal))
|
2016-07-16 23:13:33 +00:00
|
|
|
|
.Select(p => p.Name);
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static IEnumerable<string> GetPropertiesCanWritePublic(Type type)
|
2016-07-18 00:06:50 +00:00
|
|
|
|
{
|
2017-05-12 04:34:18 +00:00
|
|
|
|
return type.GetTypeInfo().DeclaredProperties
|
|
|
|
|
.Where(p => p.CanWrite && p.SetMethod.IsPublic)
|
2017-03-03 05:00:41 +00:00
|
|
|
|
.Select(p => p.Name);
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static IEnumerable<string> GetPropertiesCanWritePublicDeclared(Type type)
|
2017-03-03 05:00:41 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
return GetPropertiesCanWritePublic(type);
|
2016-07-23 08:04:55 +00:00
|
|
|
|
}
|
2017-01-08 07:54:09 +00:00
|
|
|
|
public static bool HasProperty(this Type type, string name)
|
2016-07-23 08:04:55 +00:00
|
|
|
|
{
|
2017-05-12 04:34:18 +00:00
|
|
|
|
return type.GetTypeInfo().GetDeclaredProperty(name) != null;
|
2016-09-30 22:12:23 +00:00
|
|
|
|
}
|
2017-05-23 04:55:05 +00:00
|
|
|
|
public static bool HasPropertyAll(this Type type, Type Base, string name)
|
2016-09-30 22:12:23 +00:00
|
|
|
|
{
|
2017-05-23 04:55:05 +00:00
|
|
|
|
return HasProperty(type, name) || HasProperty(Base, name);
|
2016-07-18 00:06:50 +00:00
|
|
|
|
}
|
2016-08-10 18:43:00 +00:00
|
|
|
|
|
2016-08-11 02:29:21 +00:00
|
|
|
|
private static object ConvertValue(object value, Type type)
|
2016-08-10 18:43:00 +00:00
|
|
|
|
{
|
|
|
|
|
if (type == typeof(DateTime?)) // Used for PKM.MetDate and other similar properties
|
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
return DateTime.TryParseExact(value.ToString(), "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateValue)
|
|
|
|
|
? new DateTime?(dateValue)
|
2016-08-11 02:29:21 +00:00
|
|
|
|
: null;
|
2016-08-10 18:43:00 +00:00
|
|
|
|
}
|
2016-08-11 02:29:21 +00:00
|
|
|
|
|
|
|
|
|
// Convert.ChangeType is suitable for most things
|
|
|
|
|
return Convert.ChangeType(value, type);
|
2016-08-10 18:43:00 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static bool? GetBooleanState(object obj, string prop)
|
2016-10-23 19:48:49 +00:00
|
|
|
|
{
|
|
|
|
|
return obj.GetType().HasProperty(prop) ? GetValue(obj, prop) as bool? : null;
|
|
|
|
|
}
|
2016-07-16 17:00:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|