|
| 1 | +namespace StreamJsonRpc; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// A non-generic interface with a generic method, for use with <see cref="IGenericTypeArgStore"/> |
| 5 | +/// so that generic contexts can be preserved and reused later in a non-generic context. |
| 6 | +/// </summary> |
| 7 | +internal interface IGenericTypeArgAssist |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Invokes whatever functionality the implementation provides, using the specified generic type argument. |
| 11 | + /// </summary> |
| 12 | + /// <typeparam name="T">A generic type argument, whose semantics are up to the implementation.</typeparam> |
| 13 | + /// <param name="state">Optional state that the caller may have provided.</param> |
| 14 | + /// <returns>An arbitrary result, up to the implementation.</returns> |
| 15 | + object? Invoke<T>(object? state = null); |
| 16 | +} |
| 17 | + |
| 18 | +/// <summary> |
| 19 | +/// A non-generic interface allowing invocation of a generic method with a type argument known only at runtime. |
| 20 | +/// </summary> |
| 21 | +/// <seealso cref="GenericTypeArgStore{T}"/> |
| 22 | +internal interface IGenericTypeArgStore |
| 23 | +{ |
| 24 | + /// <summary> |
| 25 | + /// Invokes the generic <see cref="IGenericTypeArgAssist.Invoke{T}(object?)"/> method. |
| 26 | + /// </summary> |
| 27 | + /// <param name="assist">An implementation of <see cref="IGenericTypeArgAssist"/>.</param> |
| 28 | + /// <param name="state">Optional state that the caller may have provided.</param> |
| 29 | + /// <returns>An arbitrary result, up to the implementation.</returns> |
| 30 | + object? Invoke(IGenericTypeArgAssist assist, object? state = null); |
| 31 | +} |
| 32 | + |
| 33 | +/// <summary> |
| 34 | +/// A generic implementation of <see cref="IGenericTypeArgStore"/> |
| 35 | +/// that can be created while in a generic context, so that a non-generic method can invoke another generic method later. |
| 36 | +/// </summary> |
| 37 | +/// <typeparam name="T">The generic type argument that must be supplied later.</typeparam> |
| 38 | +internal class GenericTypeArgStore<T> : IGenericTypeArgStore |
| 39 | +{ |
| 40 | + /// <inheritdoc/> |
| 41 | + public object? Invoke(IGenericTypeArgAssist assist, object? state = null) => assist.Invoke<T>(state); |
| 42 | +} |
0 commit comments