| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Features; |
| | | 4 | | using IceRpc.Ice.Codec; |
| | | 5 | | |
| | | 6 | | namespace IceRpc.Ice; |
| | | 7 | | |
| | | 8 | | /// <summary>Provides extension methods for <see cref="IIceProxy" /> and generated proxy structs that implement this |
| | | 9 | | /// interface.</summary> |
| | | 10 | | public static class IceProxyExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary>Converts a proxy into a proxy struct. This conversion always succeeds.</summary> |
| | | 13 | | /// <typeparam name="TProxy">The type of the target proxy struct.</typeparam> |
| | | 14 | | /// <param name="proxy">The source proxy.</param> |
| | | 15 | | /// <returns>A new instance of <typeparamref name="TProxy" />.</returns> |
| | | 16 | | public static TProxy ToProxy<TProxy>(this IIceProxy proxy) where TProxy : struct, IIceProxy => |
| | 3 | 17 | | new() { EncodeOptions = proxy.EncodeOptions, Invoker = proxy.Invoker, ServiceAddress = proxy.ServiceAddress }; |
| | | 18 | | |
| | | 19 | | /// <summary>Tests whether the target service implements the Ice interface associated with |
| | | 20 | | /// <typeparamref name="TProxy" />. This method is a wrapper for <see cref="IIceObject.IceIsAAsync" />. |
| | | 21 | | /// All services implemented with Ice automatically provide this operation. Services implemented with IceRPC provide |
| | | 22 | | /// this operation only when they implement Ice interface <c>Ice::Object</c> explicitly.</summary> |
| | | 23 | | /// <typeparam name="TProxy">The type of the target proxy struct.</typeparam> |
| | | 24 | | /// <param name="proxy">The source proxy being tested.</param> |
| | | 25 | | /// <param name="features">The invocation features.</param> |
| | | 26 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 27 | | /// <returns>A new <typeparamref name="TProxy" /> instance when <see cref="IIceObject.IceIsAAsync"/> returns |
| | | 28 | | /// <see langword="true"/>; otherwise, <see langword="null" />.</returns> |
| | | 29 | | /// <remarks>This method is equivalent to the "checked cast" methods provided by Ice. </remarks> |
| | | 30 | | public static async Task<TProxy?> AsAsync<TProxy>( |
| | | 31 | | this IIceProxy proxy, |
| | | 32 | | IFeatureCollection? features = null, |
| | | 33 | | CancellationToken cancellationToken = default) where TProxy : struct, IIceProxy => |
| | 2 | 34 | | await proxy.ToProxy<IceObjectProxy>().IceIsAAsync(typeof(TProxy).GetIceTypeId()!, features, cancellationToken) |
| | 2 | 35 | | .ConfigureAwait(false) ? |
| | 2 | 36 | | proxy.ToProxy<TProxy>() : null; |
| | | 37 | | } |