什麼是custom tool? 對visual studio比較熟悉的人, 可能都曾經注意到過, 自從VS2002開始, solution explorer中檔案的properties中, 都有一個Custom Tool屬性.
例如, 我們在VS中通過菜單Project -> Add New Item -> Data -> DataSet建立一個DataSet1.xsd檔案, 此時, 我們發現DataSet1.xsd檔案的properties中的custom tool屬性為MSDataSetGenerator.
當我們更改DataSet1.xsd檔案時, 仔細觀察, 我們發現與之相對應的DataSet1.Designer.cs檔案中也會產生做出相應的更新.
如果我們刪除此檔案, 再次儲存DataSet1.xsd, 將看到DataSet1.Designer.cs又出現了.
然後, 我們再次刪除DataSet1.Designer.cs檔案, 去掉DataSet1.xsd檔案properties中的Custom Tool屬性, 再次儲存, DataSet1.Designer.cs卻沒有出現了.
同樣, 具有Custom Tool屬性的*dbml檔案也有相同的特性.
於是, 我們有理由相信*.Designer.cs檔案的出現, 和對應檔案的Custom Tool屬性有莫大的關係.
實際上, Custom Tool屬性的作用是, 當具有此屬性的檔案在儲存時, 將調用指定的介面來處理此檔案. 例如, 根據DataSet1.xsd檔案產生相應的DataSet1.Designer.cs檔案. 另一個常見的例子是: 根據wsdl產生WebService代理類.
或許, 如果我們合理的利用此介面, 可為我們的工作剩下不少的時間.
以下是MSDN關於此介面的資料.
Implementing Single File Generators:
http://msdn2.microsoft.com/en-us/library/s686w8yb(VS.80).aspx
A custom tool — sometimes referred to as a single file generator — can be used to extend the Visual Basic, Visual C#, and Visual J# project systems in Visual Studio. A custom tool is a COM component that implements the IVsSingleFileGenerator interface. Using this interface, a custom tool transforms a single input file into a single output file. The result of the transformation may be source code, or any other output that is useful. Two examples of custom tool-generated code files are code generated in response to changes in a visual designer and files generated using Web Services Description Language (WSDL).
When a custom tool is loaded, or the input file is saved, the project system calls the Generate method, and passes a reference to a IVsGeneratorProgress callback interface, whereby the tool can report its progress to the user.
The output file that the custom tool generates is added to the project with a dependency on the input file. The project system automatically determines the name of the output file, based on the string returned by the custom tool's implementation of DefaultExtension.
A custom tool must implement the IVsSingleFileGenerator interface. Optionally, custom tools support the IObjectWithSite interface to retrieve information from sources other than the input file. In any case, before you can use a custom tool, you must register it with the system or in the Visual Studio local registry. For more information on registering custom tools, see Registering Single File Generators.
Assembly: Microsoft.VisualStudio.Shell.Interop (in microsoft.visualstudio.shell.interop.dll)