Do you want to create your own VS tool that shows up in the tools list??
VS add-in is the answer.
Create a new Add-in project.
VS does the base work for the add-in.
You have to concentrate on the execute method that is called when the tool is executed.
My goal was to create a tool that when selected will list all the controls on a form,
when a control is selected it lists all the events , when an event is selected
it automatically generates event handling code.
DOnt ask me why , its a long story.
When the tool executes , i show a new form.
I also pass in the applicationObject inorder to access the codedom.
The code generation is handled by my new form.
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using EnvDTE;
using System.Windows.Forms;
using System.Reflection;
public void Exec(string commandName, EnvDTE.vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
if(executeOption == EnvDTE.vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if(commandName == "YourTool.Connect.YourTool")
{
//write code to display the modeless dialog
YourForm _yform = new YourForm(applicationObject);
_yform.Show();
handled = true;
return;
}
}
}
private _DTE applicationObject;
..........
Portions of code from the new form.
Shows how to generate code
The fundamental building blocks of the codedom is codeelemets.
Inorder to get codeelemts , you need to access the codemodel , that can be retreived from the applicationobject ( that we passed to this form )
Once you get a codeelement , you can get get a codeclass out of it for a class.
You can then use addfunction method of the codeclass to add methods to the class.
/********************************************************************
*
* Gets the codeclass corresponding to className
*
* *******************************************************************/
private CodeClass getCodeClass(string className)
{
CodeClass ClassCodeElem = null;
CodeModel myFCM = null;
myFCM = DTE.ActiveDocument.ProjectItem.ContainingProject.CodeModel;//DTE.Solution.Projects.Item(1).CodeModel;
//DTE.ActiveDocument.ProjectItem.ContainingProject.CodeModel;
if ( null == myFCM )
{
Log.LogError("FileCodeModel is Null");
return null;
}
// get the class element.
// This is the preffered way as sugested in MSDN
// Using name might not give the desired result
CodeElements nameSpaceElems = myFCM.CodeElements;
// this gives the namespace element.
// get the class element from that
foreach ( CodeElement nameSpace in nameSpaceElems )
{
// look for the class we need
if ( nameSpace.Kind == vsCMElement.vsCMElementNamespace )
{
listBox1.Items.Add(nameSpace.Name);
CodeElements classE = ((CodeNamespace)nameSpace).Members;
foreach( CodeElement classElem in classE )
{
if ( classElem.Kind == vsCMElement.vsCMElementClass)
{
// determine if this is the class we are looking for
CodeClass classType = (CodeClass)classElem;
if ( classType.Name == className )
{
ClassCodeElem = classType;
break;
}
}
}
}
}
return ClassCodeElem;
}
Example of adding a function
brClass is a codeclass.
fnCons = brClass.AddFunction("YourFunction",vsCMFunction.vsCMFunctionFunction,vsCMTypeRef.vsCMTypeRefVoid, -1,
vsCMAccess.vsCMAccessPrivate, null);
Adding code into the function
EditPoint consRef = fnCons.EndPoint.CreateEditPoint();
// skip two lines
consRef.LineUp(1);
consRef.Insert("\n//Call Generated by tool\n;\n");
Wednesday, April 23, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment