コード例 #1
0
        /// <summary>
        /// This is used to set the named property to the specified value
        /// using Reflection.
        /// </summary>
        /// <param name="name">The name of the property to set</param>
        /// <param name="value">The value to which it is set</param>
        /// <remarks>Property name matching is case insensitive as are the
        /// values themselves.  This is used to allow setting of simple project
        /// properties (non-collection) using command line parameters in the
        /// console mode builder.</remarks>
        /// <exception cref="ArgumentNullException">This is thrown if the
        /// name parameter is null or an empty string.</exception>
        /// <exception cref="BuilderException">This is thrown if an error
        /// occurs while trying to set the named property.</exception>
        /// <returns>The parsed object value to which the property was set.</returns>
        protected object SetProperty(string name, string value)
        {
            TypeConverter tc;
            object        parsedValue;

            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            // Get all public instance properties for the project class
            if (propertyCache == null)
            {
                propertyCache = project.GetType().GetProperties(
                    BindingFlags.Public | BindingFlags.Instance);
            }

            foreach (PropertyInfo property in propertyCache)
            {
                if (String.Compare(name, property.Name,
                                   StringComparison.OrdinalIgnoreCase) == 0 && property.CanWrite)
                {
                    try
                    {
                        if (property.PropertyType.IsEnum)
                        {
                            parsedValue = Enum.Parse(property.PropertyType, value, true);
                        }
                        else
                        if (property.PropertyType == typeof(Version))
                        {
                            parsedValue = new Version(value);
                        }
                        else
                        if (property.PropertyType == typeof(FilePath))
                        {
                            parsedValue = new FilePath(value, project);
                        }
                        else
                        if (property.PropertyType == typeof(FolderPath))
                        {
                            parsedValue = new FolderPath(value, project);
                        }
                        else
                        {
                            tc          = TypeDescriptor.GetConverter(property.PropertyType);
                            parsedValue = tc.ConvertFromString(value);
                        }
                    }
                    catch (Exception ex)
                    {
                        // Ignore exceptions for the Language property.  A few people have had an environment
                        // variable with that name that gets picked up as a default and the value isn't typically
                        // valid for a culture name.
                        if (!name.Equals("Language", StringComparison.OrdinalIgnoreCase))
                        {
                            throw new BuilderException("CVT0001", "Unable to parse value '" + value +
                                                       "' for property '" + name + "'", ex);
                        }

                        parsedValue = null;
                    }

                    property.SetValue(project, parsedValue, null);
                    return(parsedValue);
                }
            }

            throw new BuilderException("CVT0002", "An attempt was made to " +
                                       "set an unknown or read-only property: " + name + "   Value: " + value);
        }