Beispiel #1
0
        /// <summary>
        /// Get the string value of the current property since all settings are stored as strings
        /// </summary>
        /// <param name="prop"></param>
        /// <param name="newVal"></param>
        /// <param name="attrib"></param>
        /// <returns></returns>
        private static string GetPropertyString(PropertyInfo prop, object newVal, SiteSettingsAttribute attrib)
        {
            string newValString = null;

            // get the current property value
            if (prop.PropertyType.IsArray)
            {
                if (newVal != null)
                {
                    newValString = string.Join(attrib.ValueDelimiter.ToString(), newVal as string[]);
                }
            }
            else
            {
                newValString = newVal?.ToString();
                if (newValString != null && newValString.Length == 0)
                {
                    newValString = null;
                }
            }
            return(newValString);
        }
Beispiel #2
0
        /// <summary>
        /// Helper method that will set the property value using reflection on the target object
        /// </summary>
        /// <param name="target"></param>
        /// <param name="prop"></param>
        /// <param name="attrib"></param>
        /// <param name="propVal"></param>
        public static void SetSiteSettingsValue(object target, PropertyInfo prop, SiteSettingsAttribute attrib, string propVal)
        {
            if (prop.PropertyType.IsArray)
            {
                prop.SetValue(target, propVal.Split(attrib.ValueDelimiter));
            }
            else
            {
                var t = prop.PropertyType.UnderlyingSystemType;
                switch (t.Name)
                {
                case "String":
                    prop.SetValue(target, propVal);
                    break;

                case "Boolean":
                    if (bool.TryParse(propVal, out bool newBool))
                    {
                        prop.SetValue(target, newBool);
                    }
                    break;

                case "Int32":
                    if (int.TryParse(propVal, out int newInt))
                    {
                        prop.SetValue(target, newInt);
                    }
                    break;

                case "TimeSpan":
                    if (TimeSpan.TryParse(propVal, out TimeSpan newTS))
                    {
                        prop.SetValue(target, newTS);
                    }
                    break;

                case @"Nullable`1":
                    if (t.FullName.Contains("System.Boolean"))
                    {
                        if (bool.TryParse(propVal, out bool nilBool))
                        {
                            prop.SetValue(target, nilBool);
                        }
                    }
                    else if (t.FullName.Contains("System.Int32"))
                    {
                        if (int.TryParse(propVal, out int nilInt))
                        {
                            prop.SetValue(target, nilInt);
                        }
                    }
                    else if (t.FullName.Contains("System.TimeSpan"))
                    {
                        if (TimeSpan.TryParse(propVal, out TimeSpan nilTS))
                        {
                            prop.SetValue(target, nilTS);
                        }
                    }
                    break;
                }
            }
        }