9

I have a project that generates text (representing an interface and a class) based on metadata. I would like to take this generated code and insert it as a new class and interface directly into the currently opened solution under a specific project and directory. I will create the menu tool that will generate the class but what I don't know how to do is gain access to the following items from within my custom Visual Studio Extension:

  1. Iterate the current solution and find a project to dump the generated code into.
  2. Open a new file window within Visual Studio and inject the generated text that comes from my tool directly into that window.
  3. Create a new folder in a specific project within the current solution from within my custom extension.

EDIT - To clarify I need to open a new file (e.g. Right Click on a Project -> Add - > New Class) and insert text into it from within my custom Visual Studio Extension.

Thanks

John Maloney
  • 1,106
  • 3
  • 15
  • 26
  • 1
    Have you considered using T4 templates instead of an extension? You can create your meta data as XML in a .config file and run the T4 template to generate each class/interface. It only works on a 1-1 meta-generated mapping though. – DaveShaw Apr 12 '12 at 15:35
  • Yes I am actually using T4 templates to generate the code now, but these classes are generated from metadata within a database so I don't want them to exist inside a project, I want to be able to let the user say which class type they want and then I generate the code (using T4) and return a class and an interface as text, which leaves me at the questions above. – John Maloney Apr 12 '12 at 16:38
  • It makes sense to put the T4 template in the same place where you will be using the generated code. – StingyJack Apr 12 '12 at 16:47
  • @JohnMaloney You can have a T4 template query a database. We have some that call a Stored Procedure to generate custom Entities and DAL code. – DaveShaw Apr 12 '12 at 17:22
  • @DaveShaw That is currently what I am doing, I am going to allow the user to right click a project and open a VS tool window that allows them to pick the type of class to generate, this will then use the T4 templates to generate the full source for that class and interface, this process is not in question, I need to know how to open a new window in Visual Studio to insert my generated text into (e.g. Right Click on a Project -> Add - > New Class) and insert text into it from within my custom Visual Studio Extension. – John Maloney Apr 12 '12 at 17:51

1 Answers1

14

For creating a new File from a Visual Studio Extension (ToolWindowPane) first use the GetService method:

// Get an instance of the currently running Visual Studio IDE
DTE dte = (DTE)GetService(typeof(DTE));

Second, ensure that a Solution is currently open, if no solution is open the File generation will not work:

string solutionDir = System.IO.Path.GetDirectoryName(dte.Solution.FullName);

Third, generate the new file from the DTE object:

dte.ItemOperations.NewFile(@"General\Visual C# Class", "ObjectOne", EnvDTE.Constants.vsViewKindTextView);

After creating the new file use the following code to access the text of that file and replace it with your generated text:

TextSelection txtSel = (TextSelection)dte.ActiveDocument.Selection;
TextDocument txtDoc = (TextDocument)dte.ActiveDocument.Object("");

txtSel.SelectAll();
txtSel.Delete();
txtSel.Insert("Hello World");
John Maloney
  • 1,106
  • 3
  • 15
  • 26
  • 2
    How to save Generated File without prompting save dialog (silent save) and add to solution as item? – Gayan Jun 09 '17 at 04:37