Ejemplo n.º 1
0
        /*
         * GetCompatibleDataObjectType
         */

        /// <summary>
        /// Returns the <see cref="Type"/> of the data associated with the specified <see cref="IDataObject"/>
        /// if it is compatible with the specified <see cref="Type"/>; otherwise, <see langword="null"/>.
        /// </summary>
        ///
        /// <param name="dataObject">
        /// Specifies the <see cref="IDataObject"/> that contains the data to check for compatability.
        /// </param>
        ///
        /// <param name="compatibleType">
        /// Specifies the <see cref="Type"/> the data associated with the <see cref="IDataObject"/>
        /// should be compatible with.
        /// </param>
        ///
        /// <returns>
        /// If the data associated with the specified <see cref="IDataObject"/> is compatible with
        /// the specified <see cref="Type"/>, returns the data type; otherwise, <see langword="null"/>.
        /// </returns>
        public static Type GetCompatibleDataObjectType(IDataObject dataObject, Type compatibleType)
        {
            if (dataObject == null)
            {
                throw new ArgumentNullException("dataObject");
            }

            if (compatibleType == null)
            {
                throw new ArgumentNullException("compatibleType");
            }

            string[] formats = dataObject.GetFormats(false);

            for (int i = 0; i < formats.Length; i++)
            {
                Type type = NuGenTypeFinder.GetType(formats[i]);

                if (NuGenArgument.IsCompatibleType(type, compatibleType))
                {
                    return(type);
                }
            }

            return(null);
        }
        /*
         * StringToObject
         */

        /// <summary>
        /// </summary>
        /// <exception cref="ArgumentNullException">
        /// <para>
        ///		<paramref name="str"/> is <see langword="null"/>.
        /// </para>
        /// -or-
        /// <para>
        ///		<paramref name="type"/> is <see langword="null"/>.
        /// </para>
        /// </exception>
        /// <exception cref="TypeLoadException">
        /// <paramref name="objectType"/> is defined in the assembly that could not be found in the current
        /// application domain.
        /// </exception>
        public virtual object StringToObject(string stringToConstructObjectFrom, Type objectType)
        {
            if (stringToConstructObjectFrom == null)
            {
                throw new ArgumentNullException("stringToConstructObjectFrom");
            }

            if (objectType == null)
            {
                throw new ArgumentNullException("objectType");
            }

            if (objectType == typeof(string))
            {
                return(stringToConstructObjectFrom);
            }

            if (objectType == typeof(decimal))
            {
                return(decimal.Parse(stringToConstructObjectFrom, CultureInfo.CurrentCulture));
            }

            if (objectType == typeof(Type))
            {
                Type typeFromStr = NuGenTypeFinder.GetType(stringToConstructObjectFrom);

                if (typeFromStr != null)
                {
                    return(typeFromStr);
                }
                else
                {
                    throw new TypeLoadException(
                              string.Format(CultureInfo.InvariantCulture, Properties.Resources.TypeLoad_NotFound, stringToConstructObjectFrom)
                              );
                }
            }

            return(TypeDescriptor.GetConverter(objectType).ConvertFromString(stringToConstructObjectFrom));
        }