用过VS.NET的朋友应该会发现,在编辑一些文件时VS会在文件下面自动创建它的附属文件.而这些附属文件往往是根据设计文件生成的代码文件来的.对于我们想实现这样的功能怎办呢?其实MS早就为我们想好了,只要简单地实现IVsSingleFileGenerator;说是简单不过还是要做些功夫的,就是把编写后VsSingleFileGenerator注册到共公程序集中,然后在注册表里添加一些东西才行.下面介绍自己实现NClay实体设计的SingleFileGenerator,有需要的朋友可以参考代码实现自己的SingleFileGenerator:)

实现目的编写XML模型描述后自动生成附属C#代码文件.

XML设计文件:

<?xml version="1.0" encoding="utf-8" ?>

<nclay_models xmlns="http://nclay.cn/model.xsd" namespace="Blogs.Entities">

  <class name="User" table="TUser" comment="">

    <id name="UserID" type="System.String"/>

    <property name="UserName" type="System.String"/>

    <property name="UserPWD" type="System.String"/>

    <property name="EMail" type="System.String"/>

    <property name="Enabled" type="System.String"/>

    <property name ="Remark" type="System.String"/>

  </class>

</nclay_models>

生成代码模型文件内容:

实现自定义的VsSingleFileGeneratorusing System;
实现自定义的VsSingleFileGenerator    
using System.Data;
实现自定义的VsSingleFileGenerator    
using NClay.Data;
实现自定义的VsSingleFileGenerator    
using NClay.Data.Mappings;
实现自定义的VsSingleFileGenerator    
实现自定义的VsSingleFileGenerator    
实现自定义的VsSingleFileGenerator    [TableMapper(Name
="User")]
    }

对于SingleFileGenerator的编写我直接就贴代码,其实也没什么好讲就一个类.

实现自定义的VsSingleFileGeneratorusing System;
实现自定义的VsSingleFileGenerator
using System.Collections.Generic;
实现自定义的VsSingleFileGenerator
using System.Text;
实现自定义的VsSingleFileGenerator
using System.Runtime.InteropServices;
实现自定义的VsSingleFileGenerator
using Microsoft.VisualStudio.Shell.Interop;
实现自定义的VsSingleFileGenerator
using System.ComponentModel;
实现自定义的VsSingleFileGenerator
using System.CodeDom.Compiler;
实现自定义的VsSingleFileGenerator
using Microsoft.VisualStudio.Shell;
实现自定义的VsSingleFileGenerator
using VSOLE = Microsoft.VisualStudio.OLE.Interop;
实现自定义的VsSingleFileGenerator
using System.CodeDom;
实现自定义的VsSingleFileGenerator
using System.IO;
实现自定义的VsSingleFileGenerator
using System.Xml;
实现自定义的VsSingleFileGenerator
实现自定义的VsSingleFileGenerator
namespace NClay.Generators

接下来就是注册

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\CLSID\{2F6150C6-BC48-4733-96FE-91F2A90AADCF}]

@="NClay.Generators.ModelGenerator"

"InprocServer32"="d:\\windows\\system32\\mscoree.dll"

"Class"="NClay.Generators.ModelGenerator"

"Assembly"="NClay.Generators, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8c768ba656ce9125"

"ThreadingModel"="Both"

 

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\Generators\{164B10B9-B200-11D0-8C61-00A0C91E29D5}\NClayGenerator]

@="NClay Model Generator"

"CLSID"="{2F6150C6-BC48-4733-96FE-91F2A90AADCF}"

"GeneratesDesignTimeSource"=dword:00000001

 

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\Generators\{E6FDF8B0-F3D1-11D4-8576-0002A516ECE8}\NClayGenerator]

@="NClay Model Generator"

"CLSID"="{2F6150C6-BC48-4733-96FE-91F2A90AADCF}"

"GeneratesDesignTimeSource"=dword:00000001

 

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\Generators\{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}\NClayGenerator]

@="NClay Model Generator"

"CLSID"="{2F6150C6-BC48-4733-96FE-91F2A90AADCF}"

"GeneratesDesignTimeSource"=dword:00000001

所有CLSID对应的值是实现IVsSingleFileGeneratorGuid描述.(记住要所DLL注册到全局程序集中)

这样SingleFileGenerator就完成了,VS中使用这个SingleFileGenerator.在解决方案管理器右键文件属性,在自定义工具填写上: NclayGenerator

在这个应用中也许有朋友想这样编写XML很麻烦,又没有编写提示和验证等功能.其实MS也为我们想好了,只需要写个XmlSchema就可以了.下载代码里也有考参文件.

 实现自定义的VsSingleFileGenerator

 

下载代码

参考资料:

其实在google 搜一下IVsSingleFileGenerator就有一堆资料

多谢timiil告诉我IVsSingleFileGenerator的功能.

相关文章: