/// <summary> /// 根据xsd生成model /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ModelCommandInvoke(object sender, EventArgs e) { //可以选多个 var items = ProjectHelpers.GetSelectedItemPaths(_dte); if (items != null && items.Any(r => !r.ToLower().EndsWith(".xsd", StringComparison.OrdinalIgnoreCase))) { ProjectHelpers.AddError(_package, "select file is not xsd file"); return; } var _file = items.ElementAt(0); var project = ProjectHelpers.GetActiveProject(); var projectFullName = ProjectHelpers.GetActiveProject().FullName; var projectInfo = new FileInfo(projectFullName); var folderproject = projectInfo.Directory?.FullName; try { string[] dataContractFiles = items.ToArray(); XsdCodeGenDialog dialogForm = new XsdCodeGenDialog(dataContractFiles); if (!project.IsWebProject()) { dialogForm.Namespace = project.GetProjectProperty("DefaultNamespace"); if (string.IsNullOrEmpty(dialogForm.Namespace)) { dialogForm.Namespace = Path.GetFileNameWithoutExtension(_file); } } else { dialogForm.Namespace = Path.GetFileNameWithoutExtension(_file); } dialogForm.TargetFileName = ProjectHelpers.GetDefaultDestinationFilename(_file); if (dialogForm.ShowDialog() == DialogResult.Cancel) { return; } CodeGenOptions options = new CodeGenOptions(); options.GenerateDataContractsOnly = true; options.DataContractFiles = dataContractFiles; options.GenerateSeparateFiles = dialogForm.GenerateMultipleFiles; options.GenerateSeparateFilesEachNamespace = dialogForm.GenerateMultipleFilesEachNamespace; options.GenerateSeparateFilesEachXsd = dialogForm.GenerateSeparateFilesEachXsd; options.AscendingClassByName = dialogForm.AscendingClassByName; options.OnlyUseDataContractSerializer = dialogForm.OnlyUseDataContractSerializer; options.OverwriteExistingFiles = dialogForm.OverwriteFiles; options.EnableDataBinding = dialogForm.DataBinding; options.EnableSummaryComment = dialogForm.Comment; options.GenerateCollections = dialogForm.Collections; options.GenerateTypedLists = dialogForm.TypedList; options.EnableLazyLoading = dialogForm.LazyLoading; options.OutputFileName = dialogForm.TargetFileName; options.OutputLocation = folderproject; options.ProjectDirectory = project.FullName; options.Language = CodeLanguage.CSharp; options.ClrNamespace = dialogForm.Namespace; options.EnableSummaryComment = dialogForm.Comment; options.ProjectName = project.Name; options.EnableBaijiSerialization = dialogForm.EnableBaijiSerialization; options.AddCustomRequestInterface = dialogForm.AddCustomRequestInterface; options.CustomRequestInterface = dialogForm.CustomRequestInterface; options.ForceElementName = dialogForm.ForceElementName; options.ForceElementNamespace = dialogForm.ForceElementNamespace; options.GenerateAsyncOperations = dialogForm.GenerateAsyncOperations; options.EnableInitializeFields = true; CodeGenerator codeGenerator = new CodeGenerator(); CodeWriterOutput output = codeGenerator.GenerateCode(options); AddGeneratedFilesToProject(output); // Finally add the project references. ProjectHelpers.AddAssemblyReferences(project); MessageBox.Show("Model generation successfully completed.", "Ant.SOA.CodeGen", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { ProjectHelpers.AddError(_package, ex.ToString()); MessageBox.Show(ex.Message, "CodeGeneration", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private bool ProcessXsdCodeGenerationRequest() { try { VisualStudioProject project = currentProject; IEnumerable<VisualStudioSelectedItem> selectedItems = vsInstance.SelectedItems; if (selectedItems.Count() == 0) { MessageBox.Show( "Cannot generate code for items outside of a project.", "Web Services Contract-First code generation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return true; } foreach (VisualStudioSelectedItem selectedItem in selectedItems) { string extension = Path.GetExtension(selectedItem.FileName).ToLower(); if (extension == ".xsd" || extension == ".wsdl") continue; MessageBox.Show( "Data Contracts can only be generated for .xsd or .wsdl files.", "Web Services Contract-First code generation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return true; } string[] dataContractFiles = selectedItems.Select(i => i.FileName).ToArray(); XsdCodeGenDialog dialogForm = new XsdCodeGenDialog(dataContractFiles); if (!project.IsWebProject) { dialogForm.Namespace = project.AssemblyNamespace; } dialogForm.TargetFileName = project.GetDefaultDestinationFilename(dataContractFiles[0]); if (dialogForm.ShowDialog() == DialogResult.Cancel) { return false; } CodeGenerationOptions options = new CodeGenerationOptions(); options.GenerateDataContracts = true; options.DataContractFiles = dataContractFiles; options.GenerateProperties = dialogForm.PublicProperties; options.GenerateCollections = dialogForm.Collections; options.GenerateSeparateFiles = dialogForm.GenerateMultipleFiles; options.OverwriteExistingFiles = dialogForm.OverwriteFiles; options.AdjustCasing = dialogForm.AdjustCasing; options.EnableDataBinding = dialogForm.DataBinding; options.GenerateOrderIdentifiers = dialogForm.OrderIdentifiers; options.GenerateTypedLists = dialogForm.GenericLists; options.ClrNamespace = dialogForm.Namespace; options.OutputFileName = dialogForm.TargetFileName; options.OutputLocation = GetOutputDirectory(); options.ProjectDirectory = project.ProjectDirectory; options.Language = project.ProjectLanguage; CodeGenerator codeGenerator = new CodeGenerator(); CodeWriterOutput output = codeGenerator.GenerateCode(options); AddGeneratedFilesToProject(output); // Finally add the project references. AddAssemblyReferences(); MessageBox.Show("Code generation successfully completed.", "WSCF.Blue", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { AppLog.LogMessage(ex.ToString()); MessageBox.Show(ex.ToString(), "CodeGeneration", MessageBoxButtons.OK, MessageBoxIcon.Error); // TODO: Log the exception. //System.Diagnostics.Debugger.Break(); } return true; } // End of ProcessXsdCodeGenerationRequestFunction.