private void Set(string key, object value, Type targetType = null, SerializationMode serializationMode = SerializationMode.Xml)
        {
            if (targetType == null)
            {
                if (value is int && (int)value == 0)
                {
                    value      = false;
                    targetType = typeof(bool);
                }
                else if (value is int && (int)value == 1)
                {
                    value      = true;
                    targetType = typeof(bool);
                }
                else
                {
                    targetType = ConvertSettingsType(Check.TryCatch <SettingsType, Exception>(() => settingsStore.GetPropertyType(collectionPath, key)), value?.GetType());
                }
            }

            if (targetType.IsEnum)
            {
                targetType = typeof(int);
            }

            if (targetType == typeof(bool))
            {
                settingsStore.SetBoolean(collectionPath, key, Convert.ToBoolean(value));
            }
            else if (targetType == typeof(string))
            {
                var s = value?.ToString() ?? string.Empty;
                settingsStore.SetString(collectionPath, key, s);
            }
            else if (targetType == typeof(int))
            {
                settingsStore.SetInt32(collectionPath, key, Convert.ToInt32(value));
            }
            else
            {
                using (var ms = new MemoryStream())
                {
                    if (serializationMode == SerializationMode.Binary)
                    {
                        var serializer = new BinaryFormatter();
                        serializer.Serialize(ms, value);
                        ms.Position = 0;
                        settingsStore.SetMemoryStream(collectionPath, key, ms);
                    }
                    else if (serializationMode == SerializationMode.Xml)
                    {
                        var serializer = new XmlSerializer(targetType);
                        serializer.Serialize(ms, value);
                        string xmlContent = Encoding.UTF8.GetString(ms.ToArray());
                        settingsStore.SetString(collectionPath, key, xmlContent);
                    }
                }
            }
        }
Example #2
0
        protected static void SavePropertyIfExists(WritableSettingsStore settingsStore, string collectionName, string propertyName, Object obj)
        {
            if (settingsStore == null || !(collectionName.Length > 0) || !(propertyName.Length > 0) || obj == null)
            {
                throw new ArgumentNullException();
            }
            var mstream = SerializeToStream(obj);

            settingsStore.SetMemoryStream(collectionName, nameof(propertyName), mstream);
        }
Example #3
0
        internal void Save()
        {
            try
            {
                var collectionName = typeof(Settings).FullName;
                if (!_writableSettingsStore.CollectionExists(collectionName))
                {
                    _writableSettingsStore.CreateCollection(collectionName);
                }

                var settings = new SharpSerializerBinarySettings(BinarySerializationMode.SizeOptimized)
                {
                    IncludeAssemblyVersionInTypeName = false, IncludeCultureInTypeName = false, IncludePublicKeyTokenInTypeName = false
                };
                var serializer = new SharpSerializer(settings);
                using var m = new MemoryStream();
                serializer.Serialize(ToSerializable(), m);
                _writableSettingsStore.SetMemoryStream(collectionName, nameof(Items), m);
            }
            catch (Exception)
            {
                //
            }
        }
        /// <summary>
        /// This function is the callback used to execute a command when the a menu item is clicked.
        /// See the Initialize method to see how the menu item is associated to this function using
        /// the OleMenuCommandService service and the MenuCommand class.
        /// </summary>
        private void MenuItemCallback(object sender, EventArgs e)
        {
            var      dte         = (DTE)GetService(typeof(DTE));
            Document document    = dte.ActiveDocument;
            var      menuCommand = (OleMenuCommand)sender;
            int      commandID   = menuCommand.CommandID.ID;

            switch (commandID)
            {
            case PkgCmdIDList.cmdidAlignParameters:
            case PkgCmdIDList.cmdidReflowPoint:
            case PkgCmdIDList.cmdidReflowSelection:
                var selection = (TextSelection)document.Selection;
                selection.DTE.UndoContext.Open("CommentReflower");
                try
                {
                    switch (commandID)
                    {
                    case PkgCmdIDList.cmdidAlignParameters:
                        EditPoint finishPt;
                        if (!CommentReflowerLib.ParameterAlignerObj.go(selection.ActivePoint, out finishPt))
                        {
                            ShowMessageBox("Comment Reflower", "There is no parameter list at the cursor.");
                        }
                        break;

                    case PkgCmdIDList.cmdidReflowPoint:
                        if (!CommentReflowerLib.CommentReflowerObj.WrapBlockContainingPoint(Params, document.Name, selection.ActivePoint))
                        {
                            ShowMessageBox("Comment Reflower", "There is no comment at the cursor.");
                        }
                        break;

                    case PkgCmdIDList.cmdidReflowSelection:
                        if (!CommentReflowerLib.CommentReflowerObj.WrapAllBlocksInSelection(Params, document.Name, selection.TopPoint, selection.BottomPoint))
                        {
                            ShowMessageBox("Comment Reflower", "There are no comments in the selection.");
                        }
                        break;
                    }
                }
                catch (Exception error)
                {
                    ShowMessageBox("Comment Reflower Error", error.ToString());
                }
                finally
                {
                    selection.DTE.UndoContext.Close();
                }
                break;

            case PkgCmdIDList.cmdidSettings:
                string installPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                using (Settings settings = new Settings(Params, installPath))
                {
                    if (settings.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        Params = settings._params;
                        WritableSettingsStore userSettingsStore = SettingsManager.GetWritableSettingsStore(SettingsScope.UserSettings);
                        userSettingsStore.CreateCollection("Settings");
                        userSettingsStore.SetMemoryStream("Settings", "Params", Params.writeToXmlMemoryStream());
                    }
                }
                break;
            }
        }