question

hamberfim avatar image
0 Votes"
hamberfim asked cooldadtx answered

dynamic or parameterized comments header

Wondering if someone might direct me on if it's possible to have a comment header dynamically populated when creating new files in Visual Studio 2019.

i.e., say I create a new project the Program.cs would have this header at the top above the first 'using' statement:
($ denoted content would by dynamically populated)

/
Name : $safeprojectname$
Author : $name$
Created : $time$
Version : 1.0
OS : Windows 10, Visual Studio 2019 community
Copyright : Work based on needed
specifications or project scope
Description : This program overall description here
Input: list and describe
Output: list and describe
/

Thanks,
Anthony

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

cooldadtx avatar image
0 Votes"
cooldadtx answered

Yes and no. You can create custom item templates that you can add to any project. Those templates can be full source files with some value substituted. But you are limited to a few sets of possible values. Read more about custom item templates here.

Within an arbitrary source file you can request the editor to auto-generate a file header if you have an .editorconfig file set up. However I don't know that it supports variable replacement. Refer to this article on how to do that.

If you need something more advanced then the editor supports VS snippets. These allow you to type in something and generate some code from that. They also support limited variable expansion. Refer to this article on how to use code snippets.

If none of these work for you then you'll need to look into a third party extension. I personally use DevExpress CodeRush for this because of the templating support it has. It allows me to type in a shortcut and auto-generate code that is as simple or complex as I need. It is advanced enough that it can even use the styling rules defined in the editor to get things to work. Getting the template working can be a little trial and error but once it works it is awesome. For example I have a template that auto-generates my company's copyright header whenever I type in /copy provided I'm on a blank line. And it generates the copyright header with the current year and my name if I want. I have other templates as well. For example I can type in /x above a member but inside a doccomment and it auto-generates an exception tag for me. If I'm on a property then the exception tag is generated differently than on a method. You can get as simple or as complex as you want. However third party extensions do cost money but I think it is worth the productivity gain.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.