using System.Reflection; using System.Threading.Tasks; namespace AMWD.Common.Extensions { /// /// Extension methods for . /// [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] public static class ReflectionExtensions { /// /// Calls a method from it's reflection asynchronously without result. /// /// The to call on an object. /// The reflected instance to call the method on. /// The parameters of the called method. /// An awaitable . public static async Task CallAsync(this MethodInfo methodInfo, object obj, params object[] parameters) { var task = (Task)methodInfo.Invoke(obj, parameters); await task.ConfigureAwait(false); } /// /// Invokes a method from it's reflection asynchronously with a result. /// /// The result type, that is expected (and casted to). /// The to invoke on an object. /// The reflected instance to invoke the method on. /// The parameters of the called method. /// An awaitable with result. public static async Task InvokeAsync(this MethodInfo methodInfo, object obj, params object[] parameters) { var task = (Task)methodInfo.Invoke(obj, parameters); await task.ConfigureAwait(false); var resultPropertyInfo = task.GetType().GetProperty("Result"); return (TResult)resultPropertyInfo.GetValue(task); } } }