| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | |
|
| | 7 | | namespace Azure.Messaging.EventHubs.Amqp |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// The set of extension methods for the <see cref="Type" /> class. |
| | 11 | | /// </summary> |
| | 12 | | /// |
| | 13 | | internal static class TypeExtensions |
| | 14 | | { |
| | 15 | | /// <summary>The set of mappings from CLR types to AMQP types for property values.</summary> |
| 2 | 16 | | private static readonly IReadOnlyDictionary<Type, AmqpProperty.Type> AmqpPropertyTypeMap = new Dictionary<Type, |
| 2 | 17 | | { |
| 2 | 18 | | { typeof(byte), AmqpProperty.Type.Byte }, |
| 2 | 19 | | { typeof(sbyte), AmqpProperty.Type.SByte }, |
| 2 | 20 | | { typeof(char), AmqpProperty.Type.Char }, |
| 2 | 21 | | { typeof(short), AmqpProperty.Type.Int16 }, |
| 2 | 22 | | { typeof(ushort), AmqpProperty.Type.UInt16 }, |
| 2 | 23 | | { typeof(int), AmqpProperty.Type.Int32 }, |
| 2 | 24 | | { typeof(uint), AmqpProperty.Type.UInt32 }, |
| 2 | 25 | | { typeof(long), AmqpProperty.Type.Int64 }, |
| 2 | 26 | | { typeof(ulong), AmqpProperty.Type.UInt64 }, |
| 2 | 27 | | { typeof(float), AmqpProperty.Type.Single }, |
| 2 | 28 | | { typeof(double), AmqpProperty.Type.Double }, |
| 2 | 29 | | { typeof(decimal), AmqpProperty.Type.Decimal }, |
| 2 | 30 | | { typeof(bool), AmqpProperty.Type.Boolean }, |
| 2 | 31 | | { typeof(Guid), AmqpProperty.Type.Guid }, |
| 2 | 32 | | { typeof(string), AmqpProperty.Type.String }, |
| 2 | 33 | | { typeof(Uri), AmqpProperty.Type.Uri }, |
| 2 | 34 | | { typeof(DateTime), AmqpProperty.Type.DateTime }, |
| 2 | 35 | | { typeof(DateTimeOffset), AmqpProperty.Type.DateTimeOffset }, |
| 2 | 36 | | { typeof(TimeSpan), AmqpProperty.Type.TimeSpan }, |
| 2 | 37 | | }; |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Translates the given <see cref="Type" /> to the corresponding |
| | 41 | | /// <see cref="AmqpProperty.Type" />. |
| | 42 | | /// </summary> |
| | 43 | | /// |
| | 44 | | /// <param name="instance">The instance that this method was invoked on.</param> |
| | 45 | | /// |
| | 46 | | /// <returns>The AMQP property type that best matches the specified <paramref name="instance"/>.</returns> |
| | 47 | | /// |
| | 48 | | public static AmqpProperty.Type ToAmqpPropertyType(this Type instance) |
| | 49 | | { |
| 214 | 50 | | if (instance == null) |
| | 51 | | { |
| 2 | 52 | | return AmqpProperty.Type.Null; |
| | 53 | | } |
| | 54 | |
|
| 212 | 55 | | if (AmqpPropertyTypeMap.TryGetValue(instance, out AmqpProperty.Type amqpType)) |
| | 56 | | { |
| 174 | 57 | | return amqpType; |
| | 58 | | } |
| | 59 | |
|
| 38 | 60 | | return AmqpProperty.Type.Unknown; |
| | 61 | | } |
| | 62 | | } |
| | 63 | | } |