Exemplo n.º 1
0
        /// <summary>
        /// Configures the layout using the property/value pairs in the specified hash table.
        /// </summary>
        /// <param name="layout"></param>
        /// <param name="args"></param>
        public static void ConfigureLayout(NLayout layout, Hashtable args)
        {
            // Configure the layout
            if (args == null)
            {
                return;
            }

            Type type = layout.GetType();
            IDictionaryEnumerator enumerator = args.GetEnumerator();

            while (enumerator.MoveNext())
            {
                PropertyInfo p = type.GetProperty(enumerator.Key.ToString());
                if (p == null)
                {
                    throw new Exception(string.Format("The property {0} is not defined for the class {1}", enumerator.Key, layout.GetType().Name));
                }

                try
                {
                    object value = enumerator.Value;
                    if (p.PropertyType.Equals(FLOAT_TYPE))
                    {   // The decimal separator problem fix
                        value = ParseFloat(value);
                    }

                    value = p.PropertyType.IsEnum ?
                            Enum.Parse(p.PropertyType, enumerator.Value.ToString()) :
                            Convert.ChangeType(value, p.PropertyType);

                    p.SetValue(layout, value, null);
                }
                catch
                {
                    throw new Exception(string.Format("The value '{0}' is not valid for the {1} property",
                                                      enumerator.Value, enumerator.Key));
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Configures the layout using the property/value pairs in the specified dictionary.
        /// </summary>
        /// <param name="layout"></param>
        /// <param name="settings"></param>
        public void ConfigureLayout(NLayout layout, Dictionary <string, string> settings)
        {
            Type layoutType = layout.GetType();

            Dictionary <string, string> .Enumerator iter = settings.GetEnumerator();

            while (iter.MoveNext())
            {
                string name  = iter.Current.Key;
                string value = iter.Current.Value;

                PropertyInfo p = layoutType.GetProperty(name);
                if (p == null)
                {
                    continue;
                }

                try
                {
                    object propertyValue = null;
                    if (p.PropertyType.Equals(FloatType))
                    {
                        propertyValue = ParseFloat(value);
                    }
                    else
                    {
                        propertyValue = p.PropertyType.IsEnum ?
                                        Enum.Parse(p.PropertyType, value) :
                                        Convert.ChangeType(value, p.PropertyType);
                    }

                    p.SetValue(layout, propertyValue, null);
                }
                catch
                {
                    throw new Exception(String.Format("The value '{0}' is not valid for the '{1}' property", value, name));
                }
            }
        }