示例#1
0
        private void InitData()
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            solutionProjectItems = SolutionUnit.GetSolutionProjects(_dte.Solution);
            coreProject          = solutionProjectItems.FirstOrDefault(w => w.Name.EndsWith(".Core")).SubProject;
            applicationProject   = solutionProjectItems.FirstOrDefault(w => w.Name.EndsWith(".Application")).SubProject;

            if (coreProject == null || applicationProject == null)
            {
                throw new Exception("项目的目录结构不符合本插件的预设,不能生成代码");
            }
        }
        public void InitData()
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            if (_dte.SelectedItems.Count > 0)
            {
                SelectedItem selectedItem      = _dte.SelectedItems.Item(1);
                ProjectItem  selectProjectItem = selectedItem.ProjectItem;

                if (selectProjectItem != null)
                {
                    #region 获取出基础信息
                    //获取当前点击的类所在的项目
                    Project topProject = selectProjectItem.ContainingProject;
                    //当前类在当前项目中的目录结构
                    //string dirPath = GetSelectFileDirPath(topProject, selectProjectItem);

                    //当前类命名空间
                    string namespaceStr = selectProjectItem.FileCodeModel.CodeElements.OfType <CodeNamespace>().First().FullName;
                    //当前项目根命名空间
                    string applicationStr = "";
                    if (!string.IsNullOrEmpty(namespaceStr))
                    {
                        applicationStr = namespaceStr.Substring(0, namespaceStr.IndexOf("."));
                    }
                    //当前类
                    CodeClass codeClass = SolutionUnit.GetClass(selectProjectItem.FileCodeModel.CodeElements);
                    //当前项目类名
                    string className = codeClass.Name;
                    //当前类中文名 [Display(Name = "供应商")]
                    string classCnName = "";
                    //当前类说明 [Description("品牌信息")]
                    string classDescription = "";
                    //获取类的中文名称和说明
                    foreach (CodeAttribute classAttribute in codeClass.Attributes)
                    {
                        switch (classAttribute.Name)
                        {
                        case "Display":
                            if (!string.IsNullOrEmpty(classAttribute.Value))
                            {
                                string displayStr = classAttribute.Value.Trim();
                                foreach (var displayValueStr in displayStr.Split(','))
                                {
                                    if (!string.IsNullOrEmpty(displayValueStr))
                                    {
                                        if (displayValueStr.Split('=')[0].Trim() == "Name")
                                        {
                                            classCnName = displayValueStr.Split('=')[1].Trim().Replace("\"", "");
                                        }
                                    }
                                }
                            }
                            break;

                        case "Description":
                            classDescription = classAttribute.Value;
                            break;
                        }
                    }

                    //获取当前解决方案里面的项目列表
                    List <ProjectItem> solutionProjectItems = SolutionUnit.GetSolutionProjects(_dte.Solution);
                    #endregion

                    DtoFileModel dtoModel = GetDtoModel("", "", classCnName, namespaceStr, classDescription, "", codeClass);

                    lblSelectEntity.Text = className;
                    lblSelectEntity.Tag  = namespaceStr;
                    var chkListDataSource = dtoModel.ClassProperties.Select(s => new TextValue {
                        Text = $"{s.Name}-{s.CnName}", Value = s
                    }).ToList();
                    listSelectEntity.DataSource    = chkListDataSource;
                    listSelectEntity.DisplayMember = "Text";
                    listSelectEntity.ValueMember   = "Value";

                    var applicationProject = solutionProjectItems.Find(w => w.Name.EndsWith(".Application"));
                    var coreProject        = solutionProjectItems.Find(w => w.Name.EndsWith(".Core"));

                    var comBoxDataSource = new List <TextProjectItem>();
                    comBoxDataSource.Add(new TextProjectItem
                    {
                        Text  = applicationProject.Name,
                        Value = new ProjectItemFullPath {
                            ProjectItem = applicationProject,
                            Namespace   = applicationProject.Name
                        }
                    });

                    foreach (EnvDTE.ProjectItem item in applicationProject.SubProject.ProjectItems)
                    {
                        if (item.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFolder)
                        {
                            comBoxDataSource.Add(new TextProjectItem
                            {
                                Text  = $"  {item.Name}",
                                Value = new ProjectItemFullPath
                                {
                                    ProjectItem = item, Namespace = $"{applicationProject.Name}.{item.Name}"
                                }
                            });
                        }

                        if (item.ProjectItems.Count > 0)
                        {
                            foreach (EnvDTE.ProjectItem childItem in item.ProjectItems)
                            {
                                if (childItem.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFolder)
                                {
                                    comBoxDataSource.Add(new TextProjectItem
                                    {
                                        Text  = $"    {childItem.Name}",
                                        Value = new ProjectItemFullPath {
                                            ProjectItem = childItem,
                                            Namespace   = $"{applicationProject.Name}.{item.Name}.{childItem.Name}"
                                        }
                                    });
                                }
                            }
                        }
                    }

                    comBoxDataSource.Add(new TextProjectItem {
                        Text = coreProject.Name, Value = new ProjectItemFullPath
                        {
                            ProjectItem = coreProject,
                            Namespace   = coreProject.Name
                        }
                    });

                    foreach (EnvDTE.ProjectItem item in coreProject.SubProject.ProjectItems)
                    {
                        if (item.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFolder)
                        {
                            comBoxDataSource.Add(new TextProjectItem
                            {
                                Text = $"  {item.Name}", Value = new ProjectItemFullPath
                                {
                                    ProjectItem = item, Namespace = $"{coreProject.Name}.{item.Name}"
                                }
                            });
                        }

                        if (item.ProjectItems.Count > 0)
                        {
                            foreach (EnvDTE.ProjectItem childItem in item.ProjectItems)
                            {
                                if (childItem.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFolder)
                                {
                                    comBoxDataSource.Add(new TextProjectItem
                                    {
                                        Text  = $"    {childItem.Name}",
                                        Value = new ProjectItemFullPath
                                        {
                                            ProjectItem = childItem,
                                            Namespace   = $"{coreProject.Name}.{item.Name}.{childItem.Name}"
                                        }
                                    });
                                }
                            }
                        }
                    }

                    comboBox1.DataSource    = comBoxDataSource;
                    comboBox1.DisplayMember = "Text";
                    comboBox1.ValueMember   = "Value";
                }
            }
        }