如何在Visual Studio中开发自己的代码生成器插件
2015-11-27 08:10
621 查看
Visual Studio是美国微软公司开发的一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具,如UML工具、代码管控工具、集成开发环境(IDE)等等,且所写的目标代码适用于微软支持的所有平台.可以说.NET开发人员离不开它,它可以极大的提高编写软件的效率. Visual Studio作为一个世界级开发工具,当然支持通过插件方式对其功能进行扩展,开发人员可以定制自己的插件来进一步提升Visual Studio的功能.
访问和调用Visual Studio内置的函数和对象
执行编译
遍历解决方案中的项目
在Visual Studio IDE中定制UI
扩展Visual Studio功能...
View Exec Code
获取当前IDE激活项目的路径:
关于如何根据数据库结构生成C# Code代码,可以参加此文章.
如果发布没有错误,那么重新启动Visual Studio2012后,在项目文件上右击弹出菜单,可以看到下面的界面:
同时在菜单栏创建了一个JackWang的命令按钮和工具菜单下还添加了一个MyVS外接程序的命令按钮,如下图:
删除插件对应的.addin文件. 默认路径为..\Users\username\My Documents\Visual Studio 2012\Addins\(请根据实际情况查看具体路径)
在 Visual Studio开发人员命令行中, 输入devenv /resetaddin MyVisualStudioAddin.Connect 进行卸载(MyVisualStudioAddin.Connect 是MyVisualStudioAddin.AddIn文件中的FullClassName;
至此, add-in 不会出现在IDE中,卸载完成. 但是要完整去除必须手动删除插件对应的项目文件(如果你再次调试,可能会再次进行注册);
虽然我们不生产代码,是代码的搬运工,但是正确的打开方式是用代码去帮我们搬运代码!!!
1 什么是Add In?
所谓的add-in就是一些被Visual Studio加载到内存中运行的,能给用户提供特定功能的DLL动态链接库. 对于一般的开发情景来说,最常见的add-in用法就是可以通过.NET语言访问 DTE2 对象. DTE2是Visual Studio Automation Model的顶层对象,它具有一组接口和对象可以与 Visual Studio进行交互.DTE2可以做以下这些事情:访问和调用Visual Studio内置的函数和对象
执行编译
遍历解决方案中的项目
在Visual Studio IDE中定制UI
扩展Visual Studio功能...
2 创建VS Add In项目
用Visual Studio 2012创建名为MyVisualStudioAddin的项目(根据向导进行设置,这里不赘述),界面如下:public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled) { try { handled = false; if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault) { //命名空间.Connect.命名 if (commandName == string.Format("MyVisualStudioAddin.Connect.{0}", MY_COMMAND_FindInSolutionExplorer)) { FindCurrentActiveDocumentInSolutionExplorer(); handled = true; return; } if (commandName == string.Format("MyVisualStudioAddin.Connect.{0}", MY_COMMAND_CodeWindow)) { string fullpath = this.GetActiveProjectFullPath(); if (fullpath != "") { POCOGenerator.ConnectionForm frm = new POCOGenerator.ConnectionForm(fullpath); frm.Show(); } handled = true; return; } if (commandName == string.Format("MyVisualStudioAddin.Connect.{0}", MY_COMMAND_MenuBar)) { string fullpath = this.GetActiveProjectFullPath(); if (fullpath != "") { POCOGenerator.ConnectionForm frm = new POCOGenerator.ConnectionForm(fullpath); frm.Show(); } handled = true; return; } if (commandName == string.Format("MyVisualStudioAddin.Connect.{0}", MY_COMMAND_Project)) { string fullpath = this.GetActiveProjectFullPath(); if (fullpath != "") { POCOGenerator.ConnectionForm frm = new POCOGenerator.ConnectionForm(fullpath); frm.Show(); } handled = true; return; } if (commandName == string.Format("MyVisualStudioAddin.Connect.{0}", MY_COMMAND_Solution)) { string fullpath = this.GetActiveProjectFullPath(); if (fullpath != "") { POCOGenerator.ConnectionForm frm = new POCOGenerator.ConnectionForm(fullpath); frm.Show(); } handled = true; return; } if (commandName == string.Format("MyVisualStudioAddin.Connect.{0}", MY_COMMAND_Files)) { string fullpath = this.GetActiveProjectFullPath(); if (fullpath != "") { POCOGenerator.ConnectionForm frm = new POCOGenerator.ConnectionForm(fullpath); frm.Show(); } handled = true; return; } if (commandName == "MyVisualStudioAddin.Connect.MyVisualStudioAddin") { string fullpath = this.GetActiveProjectFullPath(); if (fullpath != "") { POCOGenerator.ConnectionForm frm = new POCOGenerator.ConnectionForm(fullpath); frm.Show(); } handled = true; return; } } } catch (Exception ex) { logger.LogError(ex.ToString()); } }
View Exec Code
获取当前IDE激活项目的路径:
/// <summary> /// Gets the Active project FullPath /// </summary> /// <returns></returns> public string GetActiveProjectFullPath() { // Returns the name of the currently selected project in the solution. Project proj = getActiveProject(); if (proj!=null) { string fullPath = proj.Properties.Item("FullPath").Value.ToString(); return fullPath; // return proj.FullName; } return ""; } /// <summary> /// Gets the Active project /// </summary> /// <returns></returns> public Project getActiveProject() { Array projects = (Array)_applicationObject.ActiveSolutionProjects; if (projects != null && projects.Length > 0) { return projects.GetValue(0) as Project; } projects = (Array)_applicationObject.Solution.SolutionBuild.StartupProjects; if (projects != null && projects.Length >= 1) { return projects.GetValue(0) as Project; } projects = (Array)_applicationObject.Solution.Projects; if (projects != null && projects.Length > 0) { return projects.GetValue(0) as Project; } return null; }
关于如何根据数据库结构生成C# Code代码,可以参加此文章.
4 插件发布
创建了外接程序后,必须先向 Visual Studio 注册此外接程序,然后才能在“外接程序管理器”中激活它。 使用具有 .addin 文件扩展名的 XML 文件来完成此操作。.addin 文件描述了 Visual Studio 在“外接程序管理器”中显示外接程序所需的信息。 在 Visual Studio 启动时,它会查找 .addin 文件位置,获取任何可用的 .addin 文件。 如果找到相应文件,则会读取 XML 文件并向“外接程序管理器”提供在单击外接程序进行启动时所需的信息。使用外接程序向导创建外接程序时,会自动创建一个 .addin 文件。 你也可以使用本主题中的信息手动创建 .addin 文件。我是用Visual Studio2012 所以将.addin文件和对应的dll拷贝到C:\Users\wangming\Documents\Visual Studio 2012\Addins文件下:如果发布没有错误,那么重新启动Visual Studio2012后,在项目文件上右击弹出菜单,可以看到下面的界面:
同时在菜单栏创建了一个JackWang的命令按钮和工具菜单下还添加了一个MyVS外接程序的命令按钮,如下图:
5 代码生成器
代码生成器(此处用的是yuvalsol的工程,我将其整合到插件中)可以根据用户选择的数据库,选择对应的表,然后生成表结构对应的C#类:6 插件卸载
如果自己定义的插件想卸载怎么办?可参见https://msdn.microsoft.com/en-us/library/ms228765.aspx.删除插件对应的.addin文件. 默认路径为..\Users\username\My Documents\Visual Studio 2012\Addins\(请根据实际情况查看具体路径)
在 Visual Studio开发人员命令行中, 输入devenv /resetaddin MyVisualStudioAddin.Connect 进行卸载(MyVisualStudioAddin.Connect 是MyVisualStudioAddin.AddIn文件中的FullClassName;
至此, add-in 不会出现在IDE中,卸载完成. 但是要完整去除必须手动删除插件对应的项目文件(如果你再次调试,可能会再次进行注册);
7 总结
通过插件机制可以方便的定制VS IDE, 一般软件公司都有自己的一套框架,其代码也有一定的封装,且各不相同,可以通过扩展VS,通过定制的代码生成工具来快速生成符合本公司所需的代码,从而从重复机械的劳动中解放出来(虽然完全自动生成的代码不可能直接能用,但是人工在此基础上进行调整,也提升了代码的编写效率,而且减少类似于拼写/标点等人为的错误点等.虽然我们不生产代码,是代码的搬运工,但是正确的打开方式是用代码去帮我们搬运代码!!!
相关文章推荐
- C语言结构体(struct)常见使用方法
- Java中PreparedStatement和Statement的用法区别
- Java编程思想学习心得(一)对象那些事儿
- 不要学习代码,要学会思考
- 我是如何阅读编程书的
- Python 安全类目推荐 (持续更新)
- Spark 跑 java 示例代码
- Google Earth API 替换方案
- Struts2学习笔记(一)--------学习工具下载、HelloWorld、运行机制
- Bullet:Python的函数中参数是引用吗?
- python的一些常用的标准库
- Got the Best Employee of the year 2015 Star Award
- 《CLR via C#》中事件一章的笔记
- 熟悉MyEclipse
- Java Thread Programming -- 目录
- 首次在树莓派上同时实现SOCKET编程与串口编程
- Spring 事务传递教程_有实例
- PHP通过反射动态加载第三方类和获得类源码的实例
- PHP编程中的__clone()方法使用详解
- 实例简介PHP的一些高级面相对象编程的特性