add Utils/Reflection
This commit is contained in:
parent
d2c83ddc7f
commit
4b8c4f1942
|
@ -0,0 +1,26 @@
|
||||||
|
namespace InnovEnergy.Lib.Utils.Reflection;
|
||||||
|
|
||||||
|
public abstract class DataMember : Member
|
||||||
|
{
|
||||||
|
public abstract Object? Get();
|
||||||
|
public abstract void Set(Object value);
|
||||||
|
|
||||||
|
public abstract Boolean IsWriteable { get; }
|
||||||
|
public abstract Boolean IsReadable { get; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class DataMembers
|
||||||
|
{
|
||||||
|
public static IEnumerable<DataMember> OfInstance<T>(T instance) where T : notnull
|
||||||
|
{
|
||||||
|
return instance.GetDataMembers();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<DataMember> GetDataMembers<T>(this T instance) where T : notnull
|
||||||
|
{
|
||||||
|
var fields = instance.GetFields();
|
||||||
|
var props = instance.GetProperties();
|
||||||
|
|
||||||
|
return fields.Concat<DataMember>(props);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
using System.Reflection;
|
||||||
|
using static System.Reflection.BindingFlags;
|
||||||
|
|
||||||
|
namespace InnovEnergy.Lib.Utils.Reflection;
|
||||||
|
|
||||||
|
public class Field : DataMember
|
||||||
|
{
|
||||||
|
private readonly Object? _Instance;
|
||||||
|
private readonly FieldInfo _FieldInfo;
|
||||||
|
|
||||||
|
public override String Name => _FieldInfo.Name;
|
||||||
|
public override Type Type => _FieldInfo.FieldType;
|
||||||
|
|
||||||
|
public override Boolean IsPublic => _FieldInfo.IsPublic;
|
||||||
|
public override Boolean IsPrivate => _FieldInfo.IsPrivate;
|
||||||
|
public override Boolean IsStatic => _FieldInfo.IsStatic;
|
||||||
|
|
||||||
|
public override Boolean IsWriteable => true;
|
||||||
|
public override Boolean IsReadable => true;
|
||||||
|
|
||||||
|
public override IEnumerable<Attribute> Attributes => _FieldInfo
|
||||||
|
.GetCustomAttributes(inherit: false)
|
||||||
|
.OfType<Attribute>();
|
||||||
|
|
||||||
|
internal Field(Object? instance, FieldInfo fieldInfo)
|
||||||
|
{
|
||||||
|
_Instance = instance;
|
||||||
|
_FieldInfo = fieldInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Object? Get() => _FieldInfo.GetValue(_Instance) ;
|
||||||
|
public override void Set(Object value) => _FieldInfo.SetValue(_Instance, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Fields
|
||||||
|
{
|
||||||
|
public static IEnumerable<Field> GetFields<T>(this T instance) where T : notnull
|
||||||
|
{
|
||||||
|
return instance
|
||||||
|
.GetType()
|
||||||
|
.GetFields(Instance | Static | Public | NonPublic)
|
||||||
|
.Select(fi => new Field(fi.IsStatic ? null : instance, fi));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<Field> OfInstance<T>(T instance) where T : notnull
|
||||||
|
{
|
||||||
|
return instance.GetFields();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
namespace InnovEnergy.Lib.Utils.Reflection;
|
||||||
|
|
||||||
|
public abstract class Member
|
||||||
|
{
|
||||||
|
public abstract String Name { get; }
|
||||||
|
public abstract Type Type { get; }
|
||||||
|
|
||||||
|
public abstract Boolean IsPublic { get; }
|
||||||
|
public abstract Boolean IsPrivate { get; }
|
||||||
|
public abstract Boolean IsStatic { get; }
|
||||||
|
|
||||||
|
public abstract IEnumerable<Attribute> Attributes { get; }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static class Members
|
||||||
|
{
|
||||||
|
public static IEnumerable<Member> OfInstance<T>(T instance) where T : notnull
|
||||||
|
{
|
||||||
|
return instance.GetMembers();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<Member> GetMembers<T>(this T instance) where T : notnull
|
||||||
|
{
|
||||||
|
var fields = instance.GetFields();
|
||||||
|
var props = instance.GetProperties();
|
||||||
|
var methods = instance.GetMethods();
|
||||||
|
|
||||||
|
return fields
|
||||||
|
.Concat<Member>(props)
|
||||||
|
.Concat(methods);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,52 @@
|
||||||
|
using System.Reflection;
|
||||||
|
using static System.Reflection.BindingFlags;
|
||||||
|
|
||||||
|
namespace InnovEnergy.Lib.Utils.Reflection;
|
||||||
|
|
||||||
|
public class Method : Member
|
||||||
|
{
|
||||||
|
private readonly MethodInfo _MethodInfo;
|
||||||
|
private readonly Object? _Instance;
|
||||||
|
|
||||||
|
public override Boolean IsPublic => _MethodInfo.IsPublic;
|
||||||
|
public override Boolean IsPrivate => _MethodInfo.IsPrivate;
|
||||||
|
public override Boolean IsStatic => _MethodInfo.IsStatic;
|
||||||
|
|
||||||
|
public override String Name => _MethodInfo.Name;
|
||||||
|
public override Type Type => _MethodInfo.ReturnType;
|
||||||
|
|
||||||
|
public override IEnumerable<Attribute> Attributes => _MethodInfo.GetCustomAttributes();
|
||||||
|
|
||||||
|
public IEnumerable<Parameter> Parameters => _MethodInfo
|
||||||
|
.GetParameters()
|
||||||
|
.Select(i => new Parameter(i));
|
||||||
|
|
||||||
|
internal Method(Object? instance, MethodInfo fieldInfo)
|
||||||
|
{
|
||||||
|
_MethodInfo = fieldInfo;
|
||||||
|
_Instance = instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object? Invoke(params Object[] parameters)
|
||||||
|
{
|
||||||
|
return _MethodInfo.Invoke(_Instance, parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static class Methods
|
||||||
|
{
|
||||||
|
public static IEnumerable<Method> GetMethods<T>(this T instance) where T : notnull
|
||||||
|
{
|
||||||
|
return typeof(T)
|
||||||
|
.GetMethods(Instance | Static | Public | NonPublic)
|
||||||
|
.Select(mi => new Method(mi.IsStatic ? null : instance, mi));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<Method> OfInstance<T>(T instance) where T : notnull
|
||||||
|
{
|
||||||
|
return instance.GetMethods();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace InnovEnergy.Lib.Utils.Reflection
|
||||||
|
{
|
||||||
|
public class Parameter
|
||||||
|
{
|
||||||
|
private readonly ParameterInfo _ParameterInfo;
|
||||||
|
|
||||||
|
internal Parameter(ParameterInfo parameterInfo) => _ParameterInfo = parameterInfo;
|
||||||
|
|
||||||
|
public Type Type => _ParameterInfo.ParameterType;
|
||||||
|
public String Name => _ParameterInfo.Name ?? "<no name>";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,87 @@
|
||||||
|
using System.Reflection;
|
||||||
|
using static System.Reflection.BindingFlags;
|
||||||
|
|
||||||
|
namespace InnovEnergy.Lib.Utils.Reflection
|
||||||
|
{
|
||||||
|
public class Property : DataMember
|
||||||
|
{
|
||||||
|
private readonly Object? _Instance;
|
||||||
|
private readonly PropertyInfo _PropertyInfo;
|
||||||
|
|
||||||
|
internal Property(Object? instance, PropertyInfo propertyInfo)
|
||||||
|
{
|
||||||
|
_Instance = instance;
|
||||||
|
_PropertyInfo = propertyInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Boolean IsWriteable => _PropertyInfo.CanWrite;
|
||||||
|
public override Boolean IsReadable => _PropertyInfo.CanRead;
|
||||||
|
public override String Name => _PropertyInfo.Name;
|
||||||
|
public override Type Type => _PropertyInfo.PropertyType;
|
||||||
|
|
||||||
|
public override Boolean IsPublic => _PropertyInfo.IsPublic();
|
||||||
|
public override Boolean IsPrivate => _PropertyInfo.IsPrivate();
|
||||||
|
public override Boolean IsStatic => _PropertyInfo.IsStatic();
|
||||||
|
|
||||||
|
public override IEnumerable<Attribute> Attributes => GetAttributes<Attribute>();
|
||||||
|
|
||||||
|
public override Object? Get() => _PropertyInfo.GetValue(_Instance);
|
||||||
|
public override void Set(Object value) => _PropertyInfo.SetValue(_Instance, value);
|
||||||
|
|
||||||
|
public IEnumerable<T> GetAttributes<T> () where T : Attribute => _PropertyInfo
|
||||||
|
.GetCustomAttributes(inherit: false)
|
||||||
|
.OfType<T>();
|
||||||
|
|
||||||
|
public Boolean HasAttribute<T> () where T : Attribute => GetAttributes<T>().Any();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static class Properties
|
||||||
|
{
|
||||||
|
public static IEnumerable<Property> GetProperties<T>(this T instance) where T : notnull
|
||||||
|
{
|
||||||
|
return instance
|
||||||
|
.GetType()
|
||||||
|
.GetProperties(Instance | Static | Public | NonPublic)
|
||||||
|
.Where(p => p.GetIndexParameters().Length == 0) // no indexers please
|
||||||
|
.Select(pi => new Property(pi.IsStatic() ? null : instance, pi));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<Property> OfInstance<T>(T instance) where T : notnull
|
||||||
|
{
|
||||||
|
return instance.GetProperties();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
internal static class PropertyInfoExtensions
|
||||||
|
{
|
||||||
|
public static Boolean IsStatic(this PropertyInfo i)
|
||||||
|
{
|
||||||
|
var getter = i.GetMethod;
|
||||||
|
var setter = i.SetMethod;
|
||||||
|
|
||||||
|
return (getter is not null && getter.IsStatic)
|
||||||
|
|| (setter is not null && setter.IsStatic);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Boolean IsPublic(this PropertyInfo i)
|
||||||
|
{
|
||||||
|
var getter = i.GetMethod;
|
||||||
|
var setter = i.SetMethod;
|
||||||
|
|
||||||
|
return (getter is not null && getter.IsPublic)
|
||||||
|
|| (setter is not null && setter.IsPublic);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Boolean IsPrivate(this PropertyInfo i)
|
||||||
|
{
|
||||||
|
var getter = i.GetMethod;
|
||||||
|
var setter = i.SetMethod;
|
||||||
|
|
||||||
|
return (getter is not null && getter.IsPrivate)
|
||||||
|
|| (setter is not null && setter.IsPrivate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue