| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | namespace IceRpc; |
| | 4 | |
|
| | 5 | | /// <summary>Provides extension methods for <see cref="Type" />.</summary> |
| | 6 | | public static class TypeExtensions |
| | 7 | | { |
| | 8 | | /// <summary>Retrieves the default service path with the attribute <see cref="DefaultServicePathAttribute"/>. |
| | 9 | | /// </summary> |
| | 10 | | /// <param name="type">The type of an interface.</param> |
| | 11 | | /// <returns>The default service path.</returns> |
| | 12 | | /// <exception cref="ArgumentException">Thrown if <paramref name="type" /> is not an interface, or if |
| | 13 | | /// it does not have a <see cref="DefaultServicePathAttribute" /> attribute.</exception> |
| | 14 | | public static string GetDefaultServicePath(this Type type) |
| 7 | 15 | | { |
| 7 | 16 | | if (type.IsInterface) |
| 7 | 17 | | { |
| 7 | 18 | | object[] attributes = type.GetCustomAttributes(typeof(DefaultServicePathAttribute), false); |
| 7 | 19 | | if (attributes.Length == 1 && attributes[0] is DefaultServicePathAttribute defaultServicePath) |
| 7 | 20 | | { |
| 7 | 21 | | return defaultServicePath.Value; |
| | 22 | | } |
| | 23 | | else |
| 0 | 24 | | { |
| 0 | 25 | | throw new ArgumentException( |
| 0 | 26 | | $"Interface '{type}' does not have a {nameof(DefaultServicePathAttribute)} attribute.", |
| 0 | 27 | | nameof(type)); |
| | 28 | | } |
| | 29 | | } |
| | 30 | | else |
| 0 | 31 | | { |
| 0 | 32 | | throw new ArgumentException($"The type '{type}' is not an interface.", nameof(type)); |
| | 33 | | } |
| 7 | 34 | | } |
| | 35 | | } |