PKHeX/Util/ReflectUtil.cs
Kaphotics a41cb8bae0 Add advanced search to database
Can run certain queries using the same format as BatchEditor

Improved property filtering if filter is invalid.
2016-07-23 01:04:55 -07:00

42 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace PKHeX
{
public static class ReflectUtil
{
internal static bool GetValueEquals(object obj, string propertyName, object value)
{
PropertyInfo pi = obj.GetType().GetProperty(propertyName);
var v = pi.GetValue(obj, null);
var c = Convert.ChangeType(value, pi.PropertyType);
return v.Equals(c);
}
internal static void SetValue(object obj, string propertyName, object value)
{
PropertyInfo pi = obj.GetType().GetProperty(propertyName);
pi.SetValue(obj, Convert.ChangeType(value, pi.PropertyType), null);
}
internal static object GetValue(object obj, string propertyName)
{
PropertyInfo pi = obj.GetType().GetProperty(propertyName);
return pi.GetValue(obj, null);
}
internal static IEnumerable<string> getPropertiesStartWithPrefix(Type type, string prefix)
{
return type.GetProperties()
.Where(p => p.Name.StartsWith(prefix))
.Select(p => p.Name);
}
internal static IEnumerable<string> getPropertiesCanWritePublic(Type type)
{
return type.GetProperties().Where(p => p.CanWrite && p.GetSetMethod(nonPublic: true).IsPublic).Select(p => p.Name);
}
internal static bool HasProperty(this Type type, string name)
{
return type.GetProperties(BindingFlags.Public | BindingFlags.Instance).Any(p => p.Name == name);
}
}
}