public void RemoveKeyFromPropertyBag(string key, IPropertyBag propertyBag)
 {
     if (propertyBag == null)
     {
         throw new ArgumentNullException("propertyBag");
     }
     propertyBag.Remove(key);
     propertyBag.Update();
 }
        public void SetInPropertyBag(string key, object value, IPropertyBag propertyBag)
        {
            if (propertyBag == null)
            {
                throw new ArgumentNullException("propertyBag");
            }

            ValidateKey(key);

            string valueAsString = "(null)";
            string typeName      = "(null)";

            try
            {
                if (value == null)
                {
                    propertyBag[key] = null;
                }
                else
                {
                    // First, attempt to get the 'tostring' value. If serialization fails, then at least
                    // in the error handler do we know 'something' about what we tried to serialize.
                    valueAsString = value.ToString();
                    typeName      = value.GetType().FullName;

                    // Now serialize the value. and set it in the PropertyBag.
                    valueAsString    = configSettingSerializer.Serialize(value.GetType(), value);
                    propertyBag[key] = valueAsString;
                }

                propertyBag.Update();

                WriteToTraceLog(propertyBag, key, valueAsString);
            }
            catch (Exception ex)
            {
                string exceptionMessage = string.Format(CultureInfo.CurrentCulture
                                                        ,
                                                        "Configsetting with key '{0}' could not be set '{1}' with type '{2}'. The technical exception was: {3}: {4}"
                                                        , key
                                                        , valueAsString
                                                        , typeName
                                                        , ex.GetType().FullName
                                                        , ex.Message);

                throw new ConfigurationException(exceptionMessage, ex);
            }
        }