public void SetPersistedValue(ConfigurationValueType Type, string ValueString) { if (_PolicyEnforced) { return; } if (Type == ConfigurationValueType.Unknown) { int index = ValueString.IndexOf(':'); if (index < 1) { throw new ArgumentException(String.Format("Bad persisted configuration value! Could not find type qualifier on {0}", ValueString)); } Type = (ConfigurationValueType)Enum.Parse(typeof(ConfigurationValueType), ValueString.Substring(0, index), true); ValueString = ValueString.Substring(index + 1); } if (_Value == null) { _Value = new ConfigurationValue(ValueString, Type); } else { _Value.PersistedType = Type; _Value.PersistedValue = ValueString; } }
/// <summary> /// Converts a persisted value back to its original data type /// </summary> /// <param name="TypeQualifiedPersistedValue">The value in its persisted state, with a prefixed type identifier.</param> /// <returns>The natural state of the value originally persisted</returns> public static object ConvertFromPersistedValue(string TypeQualifiedPersistedValue) { int index = TypeQualifiedPersistedValue.IndexOf(':'); if (index < 1) { throw new ArgumentException(String.Format("Bad persisted configuration value! Could not find type qualifier on {0}", TypeQualifiedPersistedValue)); } ConfigurationValueType type = (ConfigurationValueType)Enum.Parse(typeof(ConfigurationValueType), TypeQualifiedPersistedValue.Substring(0, index), true); string valueString = TypeQualifiedPersistedValue.Substring(index + 1); return(ConvertFromPersistedValue(valueString, type)); }
public ConfigurationValue(string name, ConfigurationValueType type, object defaultValue, IObjectDescription description, List <IObjectDescription> options = null, UserObjectType subType = UserObjectType.None) { DisplayName = name; Type = type; Description = description.Description; DetailedDescription = description.DetailedDescription; Options = options; Subtype = subType; object verifiedDefault; if (!Validate(defaultValue, out verifiedDefault)) { throw new ArgumentException("Default value is not acceptable for this type."); } DefaultValue = verifiedDefault; }
/// <summary> /// Converts a persisted value back to its original data type /// </summary> /// <param name="PersistedValue">The value in its persisted state</param> /// <param name="Type">The type of the persisted value</param> /// <returns>The natural state of the value originally persisted</returns> public static object ConvertFromPersistedValue(string PersistedValue, ConfigurationValueType Type) { switch (Type) { case ConfigurationValueType.Null: return(null); case ConfigurationValueType.Bool: return(PersistedValue == "true"); case ConfigurationValueType.Int: return(Int32.Parse(PersistedValue)); case ConfigurationValueType.Long: return(Int64.Parse(PersistedValue)); case ConfigurationValueType.Double: return(Double.Parse(PersistedValue, System.Globalization.CultureInfo.InvariantCulture)); case ConfigurationValueType.String: return(PersistedValue); case ConfigurationValueType.Timespan: return(new TimeSpan(long.Parse(PersistedValue))); case ConfigurationValueType.Datetime: return(new DateTime(long.Parse(PersistedValue))); case ConfigurationValueType.ConsoleColor: return(Enum.Parse(typeof(ConsoleColor), PersistedValue)); case ConfigurationValueType.Hashtable: string[] hashItems = PersistedValue.Split(new string[1] { "þHþ" }, StringSplitOptions.None); Hashtable tempTable = new Hashtable(); foreach (string tempValue in hashItems) { string[] tempPair = tempValue.Split(new string[1] { "þEþ" }, StringSplitOptions.None); tempTable[Base64ToUtf8(tempPair[0])] = ConvertFromPersistedValue(Base64ToUtf8(tempPair[1])); } return(tempTable); case ConfigurationValueType.Array: string[] items = PersistedValue.Split(new string[1] { "þþþ" }, StringSplitOptions.None); List <object> results = new List <object>(); foreach (string item in items) { int index = item.IndexOf(':'); if (index > 0) { results.Add(ConvertFromPersistedValue(item.Substring(index + 1), (ConfigurationValueType)Enum.Parse(typeof(ConfigurationValueType), item.Substring(0, index), true))); } } return(results.ToArray()); case ConfigurationValueType.Object: return(PSSerializer.Deserialize(Utility.UtilityHost.ExpandString(PersistedValue))); default: return("<type not supported>"); } }
/// <summary> /// Creates a value object from persisted data /// </summary> /// <param name="PersistedValue">The value that will be persisted</param> /// <param name="PersistedType">The type of the value to be persisted</param> public ConfigurationValue(string PersistedValue, ConfigurationValueType PersistedType) { this.PersistedType = PersistedType; this.PersistedValue = PersistedValue; }
public ConfigurationValue(string name, ConfigurationValueType type, object defaultValue, IObjectDescription description, List<IObjectDescription> options = null, UserObjectType subType = UserObjectType.None) { DisplayName = name; Type = type; Description = description.Description; DetailedDescription = description.DetailedDescription; Options = options; Subtype = subType; object verifiedDefault; if (!Validate(defaultValue, out verifiedDefault)) throw new ArgumentException("Default value is not acceptable for this type."); DefaultValue = verifiedDefault; }