1

How would I go about making changes to classes programmatically, such as to add comments in Visual Studio (C#) based on the class/file name?

for example

Before:

using System;
using System.Collections.Generic;
using System.Text;

namespace ChangingCode.Lib
{
    public class ChangeThisClass
    {
        public void Method()
        {

        }
    }
}

After:

using System;
using System.Collections.Generic;
using System.Text;

//This Got Added Somehow
//Is it possible?

namespace ChangingCode.Lib
{
    public class ChangeThisClass
    {
        public void Method()
        {

        }
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
AFM-Horizon
  • 129
  • 2
  • 15
  • 3
    Start your research by installing the tools to write visual studio add-ins from the visual studio installer. With that you have document objects, project objects and you can create code by simply accessing the openDocuments or any other doc in the project. – Grisgram Jan 15 '20 at 14:27
  • @Grisgram Do you mean .vsix plugins? and the related documentation? – AFM-Horizon Jan 15 '20 at 14:31
  • 2
    Possible? Definitely. Resharper can rename your class to match the file name and vice versa. – Scott Hannen Jan 15 '20 at 14:42
  • See https://stackoverflow.com/questions/48570475/how-to-generate-code-in-visual-studio-in-the-current-document – Sergey Vlasov Jan 16 '20 at 03:28

2 Answers2

1

Unfortunately, CodeDom does not come with any C# Parser implementations (although it's got an interface for that). There are however quite a few C# parsers available (see this SO answer for more information).

Suppose, you decide to go with NRefactory, then adding comments becomes a matter of changing an AST:

    var compiledUnit = ICSharpCode.NRefactory.CSharp.SyntaxTree.Parse(File.ReadAllText(@"D:\file.cs"));

    var namespaceNode = compiledUnit.Children.First(n => (n.GetType() == typeof(NamespaceDeclaration))) as NamespaceDeclaration; // find the start of namespace declaration
    var parent = namespaceNode.Parent; // get reference to file root since you're adding nodes above namespace
    parent.InsertChildAfter(namespaceNode.PrevSibling, new Comment("This Got Added Somehow"), Roles.Comment);
    parent.InsertChildAfter(namespaceNode.PrevSibling, new Comment("Is it possible?"), Roles.Comment);              
    // save it all back
    File.WriteAllText(@"D:\file.cs.modified.txt", compiledUnit.ToString());
timur
  • 14,239
  • 2
  • 11
  • 32
1

To achieve this, l think you can create a parent template which contains any custom content and then use this template in the project.

1) First, create a new C# class which is a parent template to use later and then you can add like this comment in this file:

 // this is a file called $itemname$
 //is it possible?
 //...........

And $itemname$ is a reserved template parameter and it means the name of the item created using the new template. For an example, when you use this new template and create a new item called StrFeed, the code above will show in this new file and $itemname$ will be StrFeed. More reserved template parameters, you can refer to this.

2) Click item template by Project-->Export Template and choose your custom class and then rename it like customitem and remember this name is the custom template's name and you can search this in Add New Item by its name.

3) when you finish creating this item template, please close VS Instance and then you can use this item template in other projects.

4) Create a new project and then Add New Item and search its name called customitem and then give it a new name and then you will get what you want from it.

enter image description here

For more detailed info about how to create a custom item template, you can check this link.

Mr Qian
  • 21,064
  • 1
  • 31
  • 41