junwei's profileRichard's footprint on ....PhotosBlogListsMore ![]() | Help |
|
March 28 some tips when working with VS2005 add-in developmentWhen working with VS2005 automation object model, there are several interfaces which will be absolutely frequently used. Here is the list:
DTE representing the Design Time Environment. Solution representing an opened solution in the Solution Explorer window. Project representing a project in the Solution Explorer window. ProjectItem representing a project item such as one code file in a given project. FileCodeModel representing programming element hierarchy typically against a code project item. TextSelection representing selected text in a document. TextPoint representing some text point. EditPoint representing the cursor on a given TextPoint. If you want to manipulate code file, the last three above mentioned will be of greate help. The following lists some tips from my add-in experience:
How to get reference to a solution? Solution sln = dte.Solution;
How to get reference to a project in a solution? Project proj = sln.Projects.Item(index); How to get reference to a project item in a project? ProjectItem pi = proj.ProjectItems.Item(namestring); How to get reference to FileCodeModel against a code file? FileCodeModel fcm = pi.FileCodeModel; How to get reference to TextSelection against a document? TextSelection sel = pi.Document.Selection; How to get reference to TextPoint in a document? TextPoint txtPoint = sel.ActivePoint; How to get reference to EditPoint? EditPoint edtPoint = txtPoint.CreateEditPoint(); How to determine the type of a given project? proj.Kind == PrjKind.prjKindCSharpProject; How to determine a project is a web site project? proj.Kind == VsWebSite.prjKind.PrjKindVenusProject; (requires VsWebSite.Interop.dll reference) How to determine a web site project's language type? string languageType = project.Properties.Item("CurrentWebsiteLanguage").Value.ToString(); March 06 Strongly Influenced by OthersSeveral days ago, I read W3C DOM specification. It mentioned that the DOM spec. was strongly influenced by HTML DOM. Because some persons who are responsible for drafting DOM spec. had solid software industry experience with HTML DOM resulting in the phenomenon that you can find more or less clues of HTML DOM in DOM spec. That is interesting! I am a empiricist. March 01 How to Write a VS2005 Add-InThis article focuses on vs2005 add-in (aka plug-in, since MS gives add-in as its official name, I will use the add-in term rather than plug-in throughout this article.) development. In this article, I will introduce at first background of add-in, then its rationale and how it is employed in vs2005 integrated development environment, finally I will end up with a working example demonstrating how to write a vs2005 add-in. Contents
What’s add-in To be frankly, It is just several months since I began to write my first vs2005 add-in. Prior to that, I had not tried this useful feature shipped with ms’s flagship product visual studio and even not heard of it. With the help of search engine and MSDN I’ve begun to get a clearer and clearer image of it. Add-in is time- and labor- saving application attach to and used in VS IDE, It communicates with IDE through the core automation object model (more on this later) so that you can programmatically control almost everything about the IDE such as tools window, solution explorer window and even source code to fulfill functionality what you want. The more important is add-in is integrated seamlessly in IDE so that you can run it handy and more conveniently while you are working. Before I’ve written a desktop program whose major functionality is to generate custom entity class (a bit like java entity bean except that the getter and setter methods are replaced with read/write C# property) code files against database entity table since we often want to transfer data between tiers as entity object when developing database apps. It proves useful but the frustration is I have to switch to the desktop program, run it to generate code files and then switch back to VS, include these code files in the project manually. How boring these steps are! Now, I can write an add-in that implements code file generation functionality and auto including generated files in the project. One more benefit is that it can be activated and run in VS so that you don’t have to switch back and forth between different programs. How it works From code perspective, add-in is typically an assembly implementing the IDTExtensibility2 interface which resides in the Extensibility namespace defined in Extensibility.dll. The IDTExtensibility2 interface contains methods that act as events when the interface is implemented. Visual Studio calls these methods whenever an event that affects an add-in occurs, such as when it is loaded or unloaded, whenever a change is made to it, and so forth. You can provide this interface implementation and write your own logic. As to add-in, there is an add-in definition file. It is a xml file with .AddIn extension. Add-in assembly info such as assembly name, location, friendly name, load behavior can be specified in this file. It is through this definition file that you can register add-in with vs2005. When Visual Studio starts, it looks in the .Addin File location (you can configure this through Tools->Options dialog box->Environment folder->Add-ins/Macros Security page) for any available .Addin files. If it finds any, it reads the XML file and provides the Add-In Manager (you can access it through Tools->Add-In Manager) with the information needed to start the add-in when it is clicked. When the add-in is loading, the OnConnection event represented by OnConnection method defined in the IDTExtensibility2 raised. You can get an object reference whose runtime type is DTE2 which represents the IDE design time environment and another object reference to your add-in instance. After gaining these two object reference, the subsequent process is almost like any other windows form programming. you can programmatically control by accessing the automation object model almost everything of the design time environment like solution explorer window, code editor and customize them to what you want. The automation object model consists of a few, expansive functional groups of objects. These objects control major facets of the integrated development environment (IDE) and apply to all project types in Visual Studio. Managed assembly and APIs has been provided to ease access to them. For example, Let’s assuming you’ve got a object reference txtSelection to TextSelection, then txtSelection.LineDown(1) moves the selected lines down one line. Writes your own add-in After being familiar with its rationale, it is time to write your own add-in. You don’t have to build it from scratch because vs2005 has imported an add-in wizard project. You can create an add-in project through New Project->Other Project Types->Extensibility->Visual Studio Add-in wizard. After completing a serial of query-answer steps, you will have a basic add-in code skeleton. What you need to do is add or modify the generated code to fit your project need.
Conclusion
In this article, I gave an brief introduction to vs2005 add-in. basically it is a how-to article, a summary to vs2005 add-in development and I’d like to share my experience with you. Discussions are welcome.
Related reading
1. Visual Studio Automation and Extensibility MSDN entry
http://msdn2.microsoft.com/en-us/library/xc52cke4(VS.80).aspx
2. http://www.google.com
3. A wonderful article introduction to add-in
http://www.codeproject.com/useritems/LineCounterAddin.asp
4. A good site a lot of VB add-in resources
5. Visual Studio Extensibility MSDN forum
http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=57&SiteID=1
6. Add-In Registration
http://msdn2.microsoft.com/en-us/library/19dax6cz(VS.80).aspx
|
|
|