Ejemplo 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="e">Event args.</param>
        private void Execute(object sender, EventArgs e)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            string title   = "Code Generator Completed.";
            string message = "";

            if (_dte.SelectedItems.Count > 0)
            {
                SelectedItem selectedItem      = _dte.SelectedItems.Item(1);
                ProjectItem  selectProjectItem = selectedItem.ProjectItem;
                if (selectProjectItem != null)
                {
                    //获取当前点击的类所在的项目
                    Project topProject = selectProjectItem.ContainingProject;
                    //当前类在当前项目中的目录结构
                    string    dirPath   = ProjectUtil.GetBaseProjectDirPath(topProject, selectProjectItem);
                    CodeClass codeClass = ProjectUtil.GetClass(selectProjectItem.FileCodeModel.CodeElements);
                    var       options   = GeneratorOptionsUtil.Create(codeClass, dirPath);
                    try
                    {
                        CodeGeneratorUtil.Generate(options);
                    }
                    catch (Exception ex)
                    {
                        message = ex.Message;
                    }
                }
            }

            // Show a message box to prove we were here
            VsShellUtilities.ShowMessageBox(
                this.package,
                message,
                title,
                OLEMSGICON.OLEMSGICON_INFO,
                OLEMSGBUTTON.OLEMSGBUTTON_OK,
                OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
        }