Esempio n. 1
0
        /// <summary>
        /// Applies the persisted value to the configuration item.
        /// This method should only be called by PSFramework internals
        /// </summary>
        /// <param name="Type">The type of data being specified</param>
        /// <param name="ValueString">The value string to register</param>
        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;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Converts any object into its persisted state.
        /// </summary>
        /// <param name="Item">The item to convert.</param>
        /// <returns>Its persisted state representation.</returns>
        public static ConfigurationValue ConvertToPersistedValue(object Item)
        {
            if (Item == null)
            {
                return(new ConfigurationValue("null", ConfigurationValueType.Null));
            }

            switch (Item.GetType().FullName)
            {
            case "System.Boolean":
                if ((bool)Item)
                {
                    return(new ConfigurationValue("true", ConfigurationValueType.Bool));
                }
                return(new ConfigurationValue("false", ConfigurationValueType.Bool));

            case "System.Int16":
                return(new ConfigurationValue(Item.ToString(), ConfigurationValueType.Int));

            case "System.Int32":
                return(new ConfigurationValue(Item.ToString(), ConfigurationValueType.Int));

            case "System.Int64":
                return(new ConfigurationValue(Item.ToString(), ConfigurationValueType.Long));

            case "System.UInt16":
                return(new ConfigurationValue(Item.ToString(), ConfigurationValueType.Int));

            case "System.UInt32":
                return(new ConfigurationValue(Item.ToString(), ConfigurationValueType.Long));

            case "System.UInt64":
                return(new ConfigurationValue(Item.ToString(), ConfigurationValueType.Long));

            case "System.Double":
                return(new ConfigurationValue(String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}", Item), ConfigurationValueType.Double));

            case "System.String":
                return(new ConfigurationValue(Item.ToString(), ConfigurationValueType.String));

            case "System.TimeSpan":
                return(new ConfigurationValue(((TimeSpan)Item).Ticks.ToString(), ConfigurationValueType.Timespan));

            case "System.DateTime":
                return(new ConfigurationValue(((DateTime)Item).Ticks.ToString(), ConfigurationValueType.Datetime));

            case "System.ConsoleColor":
                return(new ConfigurationValue(Item.ToString(), ConfigurationValueType.ConsoleColor));

            case "System.Collections.Hashtable":
                List <string> hashItems = new List <string>();
                Hashtable     tempTable = Item as Hashtable;
                foreach (object key in tempTable.Keys)
                {
                    hashItems.Add(String.Format("{0}þEþ{1}", Utf8ToBase64(key.ToString()), Utf8ToBase64(ConvertToPersistedValue(tempTable[key]).TypeQualifiedPersistedValue)));
                }
                return(new ConfigurationValue(String.Join("þHþ", hashItems), ConfigurationValueType.Hashtable));

            case "System.Object[]":
                List <string> items = new List <string>();

                foreach (object item in (object[])Item)
                {
                    ConfigurationValue temp = ConvertToPersistedValue(item);
                    if (temp.PersistedValue == "<type not supported>")
                    {
                        return(temp);
                    }
                    items.Add(String.Format("{0}:{1}", temp.PersistedType, temp.PersistedValue));
                }

                return(new ConfigurationValue(String.Join("þþþ", items), ConfigurationValueType.Array));

            default:
                return(new ConfigurationValue(Utility.UtilityHost.CompressString((PSSerializer.Serialize(Item))), ConfigurationValueType.Object));
            }
        }