首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

XSD To Entity:Visual Studio 2008 增益集開發實作

     編輯:關於.NET

為了專案需求,開發了一個 Visual Studio 2008 增益集,實現從 XSD 結構描述產生實體類別的功能。

安裝與使用步驟

  1. 複製檔案:將增益集組件 (.AddIn、.dll) 複製至 當前用戶\文件\Visual Studio 200X\Addins 目錄。
  2. 啟用增益集:在 VS 中開啟 工具 > 增益集管理員,勾選該增益集。
  3. 使用功能:開啟任意專案,在程式碼編輯器右鍵選單中會出現「XSD To Entity」選項。

操作介面

介面提供 XSD 檔案選擇、輸出路徑設定,點擊轉換後自動產生實體類別程式碼。

核心程式碼實作

Connect.cs - 選單整合

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom) {
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;

    // 取得程式碼視窗命令列
    CommandBar codeBar = ((CommandBars)_applicationObject.CommandBars)["Code Window"];

    // 建立彈出式選單
    CommandBarPopup popup = (CommandBarPopup)codeBar.Controls.Add(
        MsoControlType.msoControlPopup, missing, missing, 1, true);
    popup.Caption = "XSD To Entity";

    // 新增子選單項目
    CommandBarControl control = popup.Controls.Add(
        MsoControlType.msoControlButton, missing, missing, 1, true);
    control.Caption = "Execute";

    // 綁定點擊事件
    _events = (CommandBarEvents)_applicationObject.Events.get_CommandBarEvents(control);
    _events.Click += OnExecuteClick;
}

void OnExecuteClick(object cmdBarControl, ref bool handled, ref bool cancelDefault) {
    new XSDToEntityForm().ShowDialog();
}

XSDToEntityForm.cs - 呼叫 xsd.exe

Process p = new Process {
    StartInfo = {
        FileName = "cmd.exe",
        UseShellExecute = false,
        RedirectStandardInput = true,
        RedirectStandardOutput = true,
        RedirectStandardError = true,
        CreateNoWindow = true
    }
};
p.Start();
p.StandardInput.WriteLine($@"%comspec% /k ""{vsPath}"" x86");
p.StandardInput.WriteLine($@"xsd /c /language:CS {inputFile} /out:{outputPath}");
p.StandardInput.WriteLine("Exit");
p.Close();

相關 xsd.exe 指令參考

  • XML → XSD:xsd test.xml /outputdir:D:\
  • XSD → DataSet:xsd /dataset /language:CS Test.xsd(非所有 XSD 皆可轉換)
  • DLL → XSD:xsd Test.dll(非所有 DLL 皆可轉換)

此增益集封裝了命令列工具,提供圖形化介面,方便團隊快速產生實體類別。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved