為了專案需求,開發了一個 Visual Studio 2008 增益集,實現從 XSD 結構描述產生實體類別的功能。
當前用戶\文件\Visual Studio 200X\Addins 目錄。工具 > 增益集管理員,勾選該增益集。介面提供 XSD 檔案選擇、輸出路徑設定,點擊轉換後自動產生實體類別程式碼。
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();
}
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();
XML → XSD:xsd test.xml /outputdir:D:\XSD → DataSet:xsd /dataset /language:CS Test.xsd(非所有 XSD 皆可轉換)DLL → XSD:xsd Test.dll(非所有 DLL 皆可轉換)此增益集封裝了命令列工具,提供圖形化介面,方便團隊快速產生實體類別。