Search code examples
c#visual-studio-2022code-snippets

Is there a way to define custom functions for C# snippets, or at least get the namespace?


I am modernizing a lot of VB.NET code to CSharp and I need to convert dozens of dialogs to conform with our new MVP standard. I'm trying to write snippets so I can more easily bang out the boiler plate stuff but I find it annoying having to type in the interface names each time for every M/V/P, especially in the Presenter for the CTOR and private properties.

I want to do something like this: private I$namespace$View _View;

Each MVP will be in its own folder, thus in its own namespace.

I tried searching for a solution but everything seems pretty out dated (last posts being 2015/19). These were the two posts I found but they seem to only refence being able to define custom snippets for new languages, not new functions for C#.

Question about getting namespace in snippet (not for C#, but extended language)

Question about defining custom functions for snippets (very old question and answer, 10+ years)

As far as I can tell there was no way to extend this functionality, is this still the case? The first question talked about using macros within snippets to get the namespace but I couldn't find anything online about that.

Any help or documentation on creating really advanced snippets would be nice.


Solution

  • I ended up just creating a custom ItemTemplate. The customization of ItemTemplate's is more powerful in terms of what I was looking to do. Instead of having to create all the MVP files then do the boilerplate stuff I just created an ItemTemplate that does it all in one go. I found this link helpful for figuring out which template variables I could use when creating the files.