Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8835655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:18:29+00:00 2026-06-14T09:18:29+00:00

I want to programmatically add a function (a TestMethod) to an existing C#-file. After

  • 0

I want to programmatically add a function (a TestMethod) to an existing C#-file.
After some googling I have found the library EnvDTE and CodeModel.AddFunction-Method, but I can’t find a good example of what I want.

I would like to add a function with code already in that newly created function and also with an attribute. Something like this:

/// <summary>
/// Documentation
/// </summary>
[TestMethod]
public void TestMethod1()
{
    string test = Helper.CodeExample();
}

Can anyone show me an example on how to do this?

EDIT: I want to edit a C# file, like you would edit a text-file. I know you could do this with a StreamWriter, but is there maybe a better way for doing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:18:31+00:00Added an answer on June 14, 2026 at 9:18 am

    EnvDTE can be the way to go. You can develop a VisualStudio Add-In and then modify the Exec method. In this method you have to get the active document and its ProjectItem. This is where you find the CodeModel which contains a lot of CodeElements. Among these elements you have to find the CodeNamespace, and inside this element the CodeClass. This is the object that responds to AddFunction returning the new CodeFunction to which you can add your attribute and the code (this is a part I don’t like too much, since you have to use EditPoint).

    This is a very simple version of Exec that you can use as a starting point:

        public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
        {
            handled = false;
            if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
            {
                handled = true;
                if (commandName == "TestAddMethod.Connect.TestAddMethod")
                {
                    Document activeDoc = _applicationObject.ActiveDocument;
                    if (activeDoc == null)
                        return;
                    ProjectItem prjItem = activeDoc.ProjectItem;
                    if (prjItem == null)
                        return;
                    FileCodeModel fcm = prjItem.FileCodeModel;
                    if (fcm == null)
                        return;
    
                    CodeElements ces = fcm.CodeElements;
                    // look for the namespace in the active document
                    CodeNamespace cns = null;
                    foreach (CodeElement ce in ces)
                    {
                        if (ce.Kind == vsCMElement.vsCMElementNamespace)
                        {
                            cns = ce as CodeNamespace;
                            break;
                        }
                    }
                    if (cns == null)
                        return;
                    ces = cns.Members;
                    if (ces == null)
                        return;
                    // look for the first class
                    CodeClass cls = null;
                    foreach (CodeElement ce in ces)
                    {
                        if (ce.Kind == vsCMElement.vsCMElementClass)
                        {
                            cls = ce as CodeClass;
                            break;
                        }
                    }
                    if (cls == null)
                        return;
                    CodeFunction cf = cls.AddFunction("TestMethod1", vsCMFunction.vsCMFunctionFunction, vsCMTypeRef.vsCMTypeRefVoid, -1, vsCMAccess.vsCMAccessPrivate);
                    cf.AddAttribute("TestMethod", "true");
                    TextPoint tp = cf.GetStartPoint(vsCMPart.vsCMPartBody);
                    EditPoint ep = tp.CreateEditPoint();
                    ep.Indent();
                    ep.Indent();
                    ep.Indent();
                    ep.Insert("string test = Helper.CodeExample();");
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UIViewController with a XIB and want to add programmatically another subview.
I have a number of buttons and I want to add a onclick function
I have subclassed a UITableView Cell and I want to programmatically add a UITextField
I want to programmatically create a new log4j ConsoleAppender and add it as an
I want to add haptic feedback to my application's buttons and control them programmatically
I have a list definition in my solution, and i want programmatically be able
I want to programmatically create a directory on the server using ASP.NET. I have
I know I can use the wp_insert_post() function in WordPress to add posts programmatically,
I want to programmatically add class files to a c# project. Right now I'm
I want to programmatically add Text Views controls to my home screen widget. In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.