| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Diagnostics; |
| | | 4 | | |
| | | 5 | | namespace IceRpc; |
| | | 6 | | |
| | | 7 | | /// <summary>Provides extension methods for <see cref="Type" />.</summary> |
| | | 8 | | public static class TypeExtensions |
| | | 9 | | { |
| | | 10 | | /// <summary>Retrieves the default service path, as set by the attribute <see cref="DefaultServicePathAttribute"/>. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <param name="type">The interface with the <see cref="DefaultServicePathAttribute"/> attribute, or a class |
| | | 13 | | /// that implements such an interface.</param> |
| | | 14 | | /// <returns>The default service path.</returns> |
| | | 15 | | /// <exception cref="ArgumentException">Thrown if <paramref name="type" /> is neither a class nor an interface, or |
| | | 16 | | /// if it does not have a <see cref="DefaultServicePathAttribute" /> attribute, or if it is a class that implements |
| | | 17 | | /// multiple interfaces with a <see cref="DefaultServicePathAttribute" /> attribute.</exception> |
| | | 18 | | /// <remarks>When <paramref name="type" /> is an interface, this method only searches for the attribute on the |
| | | 19 | | /// interface itself.</remarks> |
| | | 20 | | /// <seealso cref="RouterExtensions.Map(Router, IDispatcher)" /> |
| | | 21 | | /// <seealso cref="RouterExtensions.Map{TService}(Router, IDispatcher)" /> |
| | | 22 | | public static string GetDefaultServicePath(this Type type) |
| | 23 | 23 | | { |
| | 23 | 24 | | if (type.IsClass) |
| | 13 | 25 | | { |
| | 13 | 26 | | string? defaultServicePath = null; |
| | | 27 | | |
| | 93 | 28 | | foreach (Type interfaceType in type.GetInterfaces()) |
| | 30 | 29 | | { |
| | 30 | 30 | | if (GetDefaultServicePathForInterface(interfaceType) is string path) |
| | 19 | 31 | | { |
| | 19 | 32 | | if (defaultServicePath is null) |
| | 13 | 33 | | { |
| | 13 | 34 | | defaultServicePath = path; |
| | 13 | 35 | | } |
| | | 36 | | else |
| | 6 | 37 | | { |
| | | 38 | | // GetInterfaces() does not return the same interface more than once. |
| | 6 | 39 | | throw new ArgumentException( |
| | 6 | 40 | | $"The class '{type}' implements multiple interfaces with a {nameof(DefaultServicePathAttribu |
| | 6 | 41 | | nameof(type)); |
| | | 42 | | } |
| | 13 | 43 | | } |
| | 24 | 44 | | } |
| | | 45 | | |
| | 7 | 46 | | return defaultServicePath ?? throw new ArgumentException( |
| | 7 | 47 | | $"The class '{type}' does not implement any interface with a {nameof(DefaultServicePathAttribute)} a |
| | 7 | 48 | | nameof(type)); |
| | | 49 | | } |
| | 10 | 50 | | else if (type.IsInterface) |
| | 10 | 51 | | { |
| | 10 | 52 | | return GetDefaultServicePathForInterface(type) ?? throw new ArgumentException( |
| | 10 | 53 | | $"The interface '{type}' does not have a {nameof(DefaultServicePathAttribute)} attribute.", |
| | 10 | 54 | | nameof(type)); |
| | | 55 | | } |
| | | 56 | | else |
| | 0 | 57 | | { |
| | 0 | 58 | | throw new ArgumentException($"The type '{type}' is neither a class nor an interface.", nameof(type)); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | static string? GetDefaultServicePathForInterface(Type type) |
| | 40 | 62 | | { |
| | 40 | 63 | | Debug.Assert(type.IsInterface); |
| | 40 | 64 | | object[] attributes = type.GetCustomAttributes(typeof(DefaultServicePathAttribute), inherit: false); |
| | 40 | 65 | | if (attributes.Length == 1) |
| | 29 | 66 | | { |
| | 29 | 67 | | return ((DefaultServicePathAttribute)attributes[0]).Value; |
| | | 68 | | } |
| | | 69 | | else |
| | 11 | 70 | | { |
| | | 71 | | // The DefaultServicePathAttribute does not allow multiple instances on the same interface. |
| | 11 | 72 | | Debug.Assert(attributes.Length == 0); |
| | 11 | 73 | | return null; |
| | | 74 | | } |
| | 40 | 75 | | } |
| | 17 | 76 | | } |
| | | 77 | | } |