< Summary

Information
Class: Nabs.Reflection.ReflectionExtensions
Assembly: Nabs.Reflection
File(s): /home/runner/work/Nabs/Nabs/src/Nabs.Reflection/ReflectionExtensions.cs
Tag: 90_14636759620
Line coverage
100%
Covered lines: 31
Uncovered lines: 0
Coverable lines: 31
Total lines: 66
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CreateInstance(...)100%11100%
InvokeMethodAsync()100%88100%
InvokeMethodAsync()100%44100%

File(s)

/home/runner/work/Nabs/Nabs/src/Nabs.Reflection/ReflectionExtensions.cs

#LineLine coverage
 1using System.Reflection;
 2
 3namespace Nabs.Reflection;
 4
 5public static class ReflectionExtensions
 6{
 7    public static T CreateInstance<T>(
 8        this Type type,
 9        params object[] args)
 10            where T : new()
 11    {
 112        var instance = Activator.CreateInstance(type, args)!;
 113        return (T)instance;
 14    }
 15
 16    public static async Task<T?> InvokeMethodAsync<T>(this Type type,
 17        string methodName,
 18        object obj,
 19        BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public,
 20        params object[] parameters)
 21    {
 122        var parametersTypes = Array.Empty<Type>();
 123        if (parameters.Length > 0)
 24        {
 225            parametersTypes = parameters.Select(_ => _.GetType()).ToArray();
 26        }
 27
 128        var methodInfo = type.GetMethod(
 129                methodName,
 130                bindingFlags,
 131                null,
 132                parametersTypes,
 133                null)
 134                ?? throw new ArgumentException($"The method, '{methodName}', could not be found or parameters did not ma
 35
 136        var task = methodInfo.Invoke(obj, parameters)!;
 37
 138        await (Task)task;
 39
 140        var resultProperty = task.GetType().GetProperty("Result")!;
 141        var result = resultProperty.GetValue(task);
 142        return result == null ? default : (T)result;
 143    }
 44
 45    public static async Task InvokeMethodAsync(this Type type,
 46            string methodName,
 47            object obj,
 48            BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public,
 49            params object[] parameters)
 50    {
 151        var parametersTypes = parameters.Length > 0
 152            ? parameters.Select(_ => _.GetType()).ToArray()
 153            : Array.Empty<Type>();
 54
 155        var methodInfo = type.GetMethod(
 156                methodName,
 157                bindingFlags,
 158                null,
 159                parametersTypes,
 160                null)
 161            ?? throw new ArgumentException($"The method, '{methodName}', could not be found or parameters did not match.
 62
 163        var task = methodInfo.Invoke(obj, parameters) as Task;
 164        await task!;
 165    }
 66}