Exemplo n.º 1
0
        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// See the constructor to see how the menu item is associated with this function using
        /// OleMenuCommandService service and MenuCommand class.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="args">Event args.</param>
        private void Execute(object sender, EventArgs args)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            ConfirmationWindow confirmationWindow = new ConfirmationWindow();
            DialogResult       buildConfirmation  = confirmationWindow.ShowDialogWithMessage("JsonButler needs to build solution to identify this type.\nProceed?");

            if (buildConfirmation != DialogResult.OK)
            {
                return;
            }

            _package.Dte?.Solution.SolutionBuild.Build(true);

            TextSelection textSelection = _package.Dte?.ActiveDocument.Selection as TextSelection;
            CodeElement   codeElement   = EditorUtilities.GetCodeElement(textSelection);
            AlertWindow   alertWindow;

            if (codeElement == null)
            {
                alertWindow = new AlertWindow();
                alertWindow.ShowDialogWithMessage("Invalid code element selected for serialization.");
                return;
            }

            ITypeResolutionService resolutionService = GetResolutionService(codeElement.ProjectItem.ContainingProject);
            Type type = resolutionService.GetType(codeElement.FullName);

            ButlerSerializerSettings serializerSettings = new ButlerSerializerSettings(type?.Assembly);

            JsonSerializerSettings     jsonSerializerSettings = new JsonSerializerSettings();
            SerializerContractResolver contractResolver       = new SerializerContractResolver();

            contractResolver.SerializationType           = _package.SerializationType;
            jsonSerializerSettings.ContractResolver      = contractResolver;
            jsonSerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            jsonSerializerSettings.Formatting            = Formatting.Indented;

            serializerSettings.JsonSerializerSettings = jsonSerializerSettings;

            string serialized = ButlerSerializer.SerializeType(type, serializerSettings);

            Clipboard.SetText(serialized);

            _package.DoAlert("Serialized JSON contents copied to clipboard.");
        }
Exemplo n.º 2
0
        public void DoAlert(string alertMessage)
        {
            _statusBar = _statusBar ?? (_statusBar = GetService(typeof(SVsStatusbar)) as IVsStatusbar);
            if (_statusBar != null)
            {
                _statusBar.IsFrozen(out int frozen);
                if (frozen == 0)
                {
                    _statusBar.SetText($"JsonButler: {alertMessage}");
                }
            }

            if (_optionPage.ShouldShowConfirmationAlerts)
            {
                AlertWindow alertWindow = new AlertWindow();
                alertWindow.ShowDialogWithMessage(alertMessage);
            }
        }