Innovenergy_trunk/csharp/Lib/Utils/Reflection/Method.cs

52 lines
1.6 KiB
C#
Raw Normal View History

2023-04-05 15:43:27 +00:00
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();
}
}