Exemplo n.º 1
0
        /// <summary>
        ///      Converts the given object to another type.  The most common types to convert
        ///      are to and from a string object.  The default implementation will make a call
        ///      to ToString on the object if the object is valid and if the destination
        ///      type is string.  If this cannot convert to the desitnation type, this will
        ///      throw a NotSupportedException.
        /// </summary>
        /// <param name='context'>
        ///      A formatter context.  This object can be used to extract additional information
        ///      about the environment this converter is being invoked from.  This may be null,
        ///      so you should always check.  Also, properties on the context object may also
        ///      return null.
        /// </param>
        /// <param name='value'>
        ///      The object to convert.
        /// </param>
        /// <param name='destinationType'>
        ///      The type to convert the object to.
        /// </param>
        /// <param name='arguments'>
        ///      An optional array of arguments to use when doing the conversion.  The arguments here
        ///      match the type and order of the Format method on UInt16.
        /// </param>
        /// <returns>
        ///      The converted object.
        /// </returns>
        public override object ConvertTo(ITypeDescriptorContext context, object value, Type destinationType, object[] arguments)
        {
            if (destinationType == null)
            {
                throw new ArgumentNullException("destinationType");
            }

            if (destinationType == typeof(string) && value is UInt16)
            {
                if (arguments == null || arguments.Length == 0)
                {
                    return(value.ToString());
                }
                else if (arguments.Length == 1)
                {
                    return(UInt16.Format((UInt16)value, (string)arguments[0]));
                }
                else
                {
                    return(UInt16.Format((UInt16)value, (string)arguments[0], (NumberFormatInfo)arguments[1]));
                }
            }

            return(base.ConvertTo(context, value, destinationType, arguments));
        }