Debug custom dll that is being referenced in visual studio macro

前端 未结 5 2118
-上瘾入骨i
-上瘾入骨i 2021-02-08 19:48

I previously asked: Add dll reference to visual studio macros

the idea of creating the macros in my language (C#) makes it easier to create the macros. The prob

5条回答
  •  无人及你
    2021-02-08 20:28

    Finally here is the solution:

    On the following steps I will describe how it will be possible to debug the dll that will be executed by a macro.

    If you want to be able to do something like:

    enter image description here (Note I am debuging a macro on c# on visual studio!!!)

    1. Create a new Solution in visual studio enter image description here

    2. Now add a new class library Project to that solution. (This is the class that will execute the macros) enter image description here

    3. Add the references EnvDTE, EbvDTE100, EnvDTE80, EnvDTE90, EnvDTE90a. Basically the same references that visual studio macros have: enter image description here

    4. Create a method that will execute the macro you plan to use on the class library.

         namespace ClassLibrary1
         {
             public static class Class1
             {
                 public static void Macro1(EnvDTE80.DTE2 DTE)
                 {
                     // make sure an active text document is open before calling this method
                     DTE.ActiveDocument.Selection.Insert("Hello World!!!");
                 }
             }
         }
      
    5. Add another project (Visual Studio Add-in)enter image description here

    6. Follow the wizzard leave the defaults except on page 4 select: enter image description here

    7. Continue selecting the default options on the wizard until the project is created: enter image description here

    8. Set that project as the startup project so that when we press f5 the addin runs.

    9. Add a reference from MyAddin1 to the class library

    10. Once we have that reference we should be able to execute the macro from the addin. In order to do so open Connect.cs and navigate to the method Exec add ClassLibrary1.Class1.Macro1(_applicationObject); so it looks like: enter image description here

    11. Add a break point at the start of the Exec method so we can debug it.

    12. Execute MyAddin1 by pressing F5. A new instance of visual studio should open.

    13. On the new instance of visual studio open any solution. In this case I am opening the same solution again>

    14. Got to tools then click on MyAddin1 but make sure a document is open: enter image description here

    15. Once you click on my addin you should hit the breakpoint! enter image description here

    15. NOTE! For some reason I had to comment the line ClassLibrary1.Class1.Macro1(_applicationObject);

    So I comment out that line and on that line I placed:

     var textDoc = (TextDocument)(_applicationObject.ActiveDocument.Object(string.Empty));
     textDoc.Selection.Insert("Hello world");
    

    finally when I click on MyAddin1 located on tools Hello world will be inserted!


    Once I know the macro is running fine I could export the class to a class library and have the macro call the method on the dll instead of the plug in.

提交回复
热议问题