Esempio n. 1
0
 /// <summary>
 /// Returns whether or not a given <see cref="SettingItem"/> can have a null-value.
 /// </summary>
 /// <param name="settingItem"></param>
 /// <returns></returns>
 public static bool CanBeNull(SettingItem settingItem)
 {
     return(!settingItem.SettingType.IsValueType);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SettingDescriptor"/> class.
 /// </summary>
 /// <param name="identifier">The identifier.</param>
 /// <param name="settingItem">The setting item.</param>
 internal SettingDescriptor(string identifier, SettingItem settingItem)
 {
     this.Identifier  = identifier;
     this.SettingItem = (SettingItem)settingItem.Clone();
 }
Esempio n. 3
0
        private static SettingsConfigurationFile ParseVersion1(XElement rootE)
        {
            string identifier = rootE.TryGetAttributeValue("Identifier", null);

            if (string.IsNullOrWhiteSpace(identifier))
            {
                return(null);
            }

            int iSetting = 0;

            List <SettingItem> settings = new List <SettingItem>();

            foreach (XElement settingE in rootE.Elements("Setting"))
            {
                // Dissect the element and retrieve all attributes
                string name = settingE.TryGetAttributeValue("Name", null);
                if (string.IsNullOrWhiteSpace(name))
                {
                    Logger.Instance.LogFormat(LogType.Warning, typeof(SettingsConfigurationFileParser), Properties.Resources.SettingItemInvalidName, iSetting + 1);
                    continue;
                }

                string typeName = settingE.TryGetAttributeValue("Type", null);
                if (string.IsNullOrWhiteSpace(typeName))
                {
                    Logger.Instance.LogFormat(LogType.Warning, typeof(SettingsConfigurationFileParser), Properties.Resources.SettingItemEmptyType, name);
                    continue;
                }
                if (!SupportedSettingTypes.Contains(typeName))
                {
                    Logger.Instance.LogFormat(LogType.Warning, typeof(SettingsConfigurationFileParser), Properties.Resources.SettingItemInvalidType, typeName, string.Join(",", SupportedSettingTypes));
                    continue;
                }

                bool isNull = settingE.TryGetAttributeValue("IsNull", false);

                // Read the setting value. If it contains a CDATA, then we need to process that first.
                string valueString = null;
                XNode  valueNode   = settingE.DescendantNodes().FirstOrDefault();
                if (valueNode != null)
                {
                    switch (valueNode.NodeType)
                    {
                    case System.Xml.XmlNodeType.CDATA:
                    case System.Xml.XmlNodeType.Text:
                        valueString = ((XText)valueNode).Value;
                        break;

                    default:
                        Logger.Instance.LogFormat(LogType.Warning, typeof(SettingsConfigurationFile), Properties.Resources.SettingsConfigurationEmbResInvalidValueContent, valueNode.NodeType, name);
                        break;
                    }
                }

                // TODO: This will work only with primitive types (String, Boolean etc.). This could be made extensible to allow storing other types as well.
                Type   type         = Type.GetType(typeName);
                object defaultValue = Convert.ChangeType(valueString, type, CultureInfo.InvariantCulture);

                SettingItem settingItem = new SettingItem(name, defaultValue, defaultValue, type);
                settings.Add(settingItem);

                iSetting++;
            }


            return(new SettingsConfigurationFile(identifier, settings));
        }