Exemple #1
0
        /// <summary>
        /// Decodes a value in <paramref name="buffer"/> starting at <paramref name="offset"/>.
        /// </summary>
        /// <typeparam name="T">The type of the value to decode.</typeparam>
        /// <param name="buffer">The buffer that contains the encoded data.</param>
        /// <param name="offset">The offset within the buffer to begin decoding from.</param>
        /// <returns>Returns the decoded value in <paramref name="buffer"/> starting at <paramref name="offset"/>.</returns>
        /// <exception cref="ArgumentException">
        /// Thrown when the specified <paramref name="T"/> has no associated mapping.
        /// </exception>
        public static T To <T>(byte[] buffer, int offset)
        {
            Type type = typeof(T);

            if (!CONVERT_TO_DELEGATE_MAPPING.ContainsKey(type))
            {
                throw new ArgumentException("Could not find delegate mapping for generic type");
            }

            ConvertToDelegate <T> convertDelegate = (ConvertToDelegate <T>)CONVERT_TO_DELEGATE_MAPPING[type];

            return(convertDelegate(buffer, offset));
        }
Exemple #2
0
        /// <summary>
        /// Registers a <see cref="ConvertToDelegate{T}"/> for the <paramref name="T"/> type.
        /// </summary>
        /// <typeparam name="T">The value type.</typeparam>
        /// <param name="convertToDelegate">The delegate that decodes the <paramref name="T"/> type.</param>
        /// <exception cref="ArgumentNullException">Thrown when the specified <paramref name="convertToDelegate"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">Thrown when the specified <paramref name="T"/> is already registered.</exception>
        public static void RegisterConvertToDelegate <T>(ConvertToDelegate <T> convertToDelegate)
        {
            if (convertToDelegate == null)
            {
                throw new ArgumentNullException("convertToDelegate");
            }

            Type type = typeof(T);

            if (CONVERT_TO_DELEGATE_MAPPING.ContainsKey(type))
            {
                throw new ArgumentException("Duplicate delegate registration for generic type");
            }

            CONVERT_TO_DELEGATE_MAPPING.Add(type, convertToDelegate);
        }