dynamic or parameterized comments header

Hamberfim 21 Reputation points
2021-04-09T16:13:13.423+00:00

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

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,292 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 56,946 Reputation points
    2021-04-09T20:13:12.86+00:00

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.