You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.8 KiB
94 lines
2.8 KiB
1 year ago
|
using System.Reflection;
|
||
|
|
||
|
namespace EC.Util.Common;
|
||
|
|
||
|
public class ReflectUtil
|
||
|
{
|
||
|
private static readonly BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
||
|
|
||
|
public static bool ContainProperty(object instance, string propertyName)
|
||
|
{
|
||
|
if (instance == null || string.IsNullOrEmpty(propertyName)) return false;
|
||
|
PropertyInfo? property = instance.GetType()?.GetProperty(propertyName, bindingAttr);
|
||
|
return property != null;
|
||
|
}
|
||
|
|
||
|
public static T? GetProperty<T>(object instance, string propertyName)
|
||
|
{
|
||
|
if (instance == null || string.IsNullOrEmpty(propertyName)) return default;
|
||
|
PropertyInfo? property = instance.GetType()?.GetProperty(propertyName, bindingAttr);
|
||
|
try
|
||
|
{
|
||
|
return (T?)property?.GetValue(instance, null);
|
||
|
}
|
||
|
catch (Exception)
|
||
|
{
|
||
|
return default;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static bool GetProperty<T>(object instance, string propertyName, out T? val)
|
||
|
{
|
||
|
val = GetProperty<T>(instance, propertyName);
|
||
|
return val != null;
|
||
|
}
|
||
|
|
||
|
public static bool ContainField(object instance, string propertyName)
|
||
|
{
|
||
|
if (instance == null || string.IsNullOrEmpty(propertyName)) return false;
|
||
|
FieldInfo? field = instance.GetType()?.GetField(propertyName, bindingAttr);
|
||
|
return field != null;
|
||
|
}
|
||
|
|
||
|
public static T? GetField<T>(object instance, string propertyName)
|
||
|
{
|
||
|
if (instance == null || string.IsNullOrEmpty(propertyName)) return default;
|
||
|
FieldInfo? field = instance.GetType()?.GetField(propertyName, bindingAttr);
|
||
|
try
|
||
|
{
|
||
|
return (T?)field?.GetValue(instance);
|
||
|
}
|
||
|
catch (Exception)
|
||
|
{
|
||
|
return default;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static bool GetField<T>(object instance, string propertyName, out T? val)
|
||
|
{
|
||
|
val = GetField<T>(instance, propertyName);
|
||
|
return val != null;
|
||
|
}
|
||
|
|
||
|
public static void SetField<T>(object instance, string propertyName, T? val)
|
||
|
{
|
||
|
if (instance == null || string.IsNullOrEmpty(propertyName)) return;
|
||
|
FieldInfo? field = instance.GetType()?.GetField(propertyName, bindingAttr);
|
||
|
try
|
||
|
{
|
||
|
field?.SetValue(instance, val);
|
||
|
}
|
||
|
catch (Exception)
|
||
|
{
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static MethodInfo? GetMethod(object instance, string name)
|
||
|
{
|
||
|
MethodInfo? method = instance.GetType().GetMethod(name);
|
||
|
return method;
|
||
|
}
|
||
|
|
||
|
public static T? RunMethod<T>(object instance, string name, object?[]? parameters)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
MethodInfo? method = instance.GetType().GetMethod(name);
|
||
|
return (T?)method?.Invoke(instance, parameters);
|
||
|
}
|
||
|
catch (Exception)
|
||
|
{
|
||
|
return default;
|
||
|
}
|
||
|
}
|
||
|
}
|