| | | 1 | | using System.Reflection; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Default implementation of <see cref="IMethodInvocation"/> used by generated |
| | | 7 | | /// interceptor proxy classes. |
| | | 8 | | /// </summary> |
| | | 9 | | public sealed class MethodInvocation : IMethodInvocation |
| | | 10 | | { |
| | | 11 | | private readonly Func<ValueTask<object?>> _proceed; |
| | | 12 | | private bool _proceeded; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Initializes a new instance of the <see cref="MethodInvocation"/> class. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="target">The target service instance.</param> |
| | | 18 | | /// <param name="method">The method being invoked.</param> |
| | | 19 | | /// <param name="arguments">The arguments passed to the method.</param> |
| | | 20 | | /// <param name="genericArguments">The generic type arguments (empty for non-generic methods).</param> |
| | | 21 | | /// <param name="proceed"> |
| | | 22 | | /// A function that invokes the next interceptor or the actual method. |
| | | 23 | | /// </param> |
| | 25 | 24 | | public MethodInvocation( |
| | 25 | 25 | | object target, |
| | 25 | 26 | | MethodInfo method, |
| | 25 | 27 | | object?[] arguments, |
| | 25 | 28 | | Type[] genericArguments, |
| | 25 | 29 | | Func<ValueTask<object?>> proceed) |
| | | 30 | | { |
| | 25 | 31 | | Target = target ?? throw new ArgumentNullException(nameof(target)); |
| | 24 | 32 | | Method = method ?? throw new ArgumentNullException(nameof(method)); |
| | 23 | 33 | | Arguments = arguments ?? throw new ArgumentNullException(nameof(arguments)); |
| | 22 | 34 | | GenericArguments = genericArguments ?? throw new ArgumentNullException(nameof(genericArguments)); |
| | 22 | 35 | | _proceed = proceed ?? throw new ArgumentNullException(nameof(proceed)); |
| | 21 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Initializes a new instance of the <see cref="MethodInvocation"/> class |
| | | 40 | | /// for non-generic methods. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="target">The target service instance.</param> |
| | | 43 | | /// <param name="method">The method being invoked.</param> |
| | | 44 | | /// <param name="arguments">The arguments passed to the method.</param> |
| | | 45 | | /// <param name="proceed"> |
| | | 46 | | /// A function that invokes the next interceptor or the actual method. |
| | | 47 | | /// </param> |
| | | 48 | | public MethodInvocation( |
| | | 49 | | object target, |
| | | 50 | | MethodInfo method, |
| | | 51 | | object?[] arguments, |
| | | 52 | | Func<ValueTask<object?>> proceed) |
| | 24 | 53 | | : this(target, method, arguments, Type.EmptyTypes, proceed) |
| | | 54 | | { |
| | 20 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | 1 | 58 | | public object Target { get; } |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | 11 | 61 | | public MethodInfo Method { get; } |
| | | 62 | | |
| | | 63 | | /// <inheritdoc /> |
| | 10 | 64 | | public object?[] Arguments { get; } |
| | | 65 | | |
| | | 66 | | /// <inheritdoc /> |
| | 4 | 67 | | public Type[] GenericArguments { get; } |
| | | 68 | | |
| | | 69 | | /// <inheritdoc /> |
| | | 70 | | public ValueTask<object?> ProceedAsync() |
| | | 71 | | { |
| | 15 | 72 | | if (_proceeded) |
| | | 73 | | { |
| | 1 | 74 | | throw new InvalidOperationException( |
| | 1 | 75 | | "ProceedAsync has already been called on this invocation. " + |
| | 1 | 76 | | "Each interceptor should call ProceedAsync at most once."); |
| | | 77 | | } |
| | 14 | 78 | | _proceeded = true; |
| | 14 | 79 | | return _proceed(); |
| | | 80 | | } |
| | | 81 | | } |