Modifying sourcecode with an addin.

I am trying to use an addin in visual studio to add code to an existing class. I am not sure how I go about doing this. I have tried the textselection class which might be the correct direction? but the textselection.TopPoint is readonly apparently so that makes life difficult.

This is the code I am trying to use:

Code Snippet

CodeElements elementList = project.ProjectItems.Item("Event_Handling").ProjectItems.Item("Event_Loop.cs").FileCodeModel.CodeElements;

//the last element is most likely the namespace so get that.
CodeElement lastElement = elementList.Item(elementList.Count-1);

CodeElements methods = lastElement.Children.Item(0).Children;

TextSelection ts = (TextSelection)project.ProjectItems.Item("Event_Handling").ProjectItems.Item("Event_Loop.cs").Document.Selection;

ts.TopPoint = methods.Item("two").GetStartPoint(vsCMPart.vsCMPartBody);
ts.BottomPoint = methods.Item("two").GetEndPoint(vsCMPart.vsCMPartBody);

ts.Text = "hello world 1";


This is the code that it is trying to operate on:

Code Snippet

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

namespace CaptureEventsSpike.Event_Handling
{
class Event_Loop
{
public void one()
{

}

public void two()
{

}

public void three()
{

}
}
}


Thanks for any help in advance. I am open to anyway of doing this. I am only using the technique above as its the only way that I can vaguely see of doing it. Using the code object model is good if I can. That is why i dont want to use the replace text function. Also the replace text function would not work for inserting code over an empty string.

[2085 byte] By [Dessus] at [2008-1-7]
# 1

Once you have a TextPoint at the desired insertion point, create an EditPoint with TextPoint.CreateEditPoint(). The EditPoint has methods to insert text, etc. that the TextPoint lacks.

CarlosQuintero-MVP at 2007-10-2 > top of Msdn Tech,Visual Studio,Visual Studio Extensibility...

Visual Studio

Site Classified