visual studio - custom tool 的相关资料

  什么是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却没有出现了.

image

同样, 具有Custom Tool属性的*dbml文件也有相同的特性.

image

于是, 我们有理由相信*.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)

posted @ 2007-10-29 16:13 rex xiang 阅读(...) 评论(...) 编辑 收藏