here is the code of the class I just created.
string classTemplate = solution2.GetProjectItemTemplate("Class", "csharp");
ProjectItem projectItemClass2 =
projectItem.ProjectItems.AddFromTemplate(classTemplate, table.Name + ".cs");
Now I want to add a region to the class then some functions to the region.
How do I do this?
Thomas Sinclair - 13 Apr 2007 00:29 GMT
Hi, Dice
The Microsoft samples are good to get you started with the basics. They can
be found at msdn.microsoft.com, click on "Visual Studio" in the left pane
and then Downloads in the left pane again. The samples you are after are
titled "Download Automation Samples for Visual Studio".
To answer your question more directly:
Once you have a ProjectItem you need to create a TextDocument here is a C#
code snippet.
mTextDoc = (TextDocument)projectItem.Document.Object("TextDocument"); //
This will give you access to the source code.
mTextDoc is a TextDocument object.
Once you have the text document you can use EditPoints to manipulate the
actual source to add your methods and regions.
Also look at the CodeModel object in the EnvDTE namespace. I don't use it
myself because I think it's too restrictive, but it is good if you don't
want to code the functionality yourself.
If you were not aware Microsoft has an Extensibility Center (link is on the
Visual Studio site mentioned above) with lots of information, consider
signing up for the VSIP partner program.
> here is the code of the class I just created.
>
[quoted text clipped - 7 lines]
> Now I want to add a region to the class then some functions to the region.
> How do I do this?
andi.reidies@googlemail.com - 26 May 2007 13:10 GMT
> here is the code of the class I just created.
>
[quoted text clipped - 5 lines]
> Now I want to add a region to the class then some functions to the region.
> How do I do this?
For inserting static text (like the #region #endregion) you have to
create an edit-point - e.g.
<CodeElement>.StartPoint.CreateEditPoint().Insert ( <language
dependent stmt> ) or
<CodeElement>.EndPoint.CreateEditPoint().Insert ( <language
dependent stmt> ).
To get access to the code element (that is: the class, a function, a
property ....) you have to use the FileCodeModel property on the
project item:
/// <summary>
/// searches for the class code model in the project item
/// </summary>
/// <param name="n">the project item of interest</param>
/// <returns>the pointer to the CodeClass definition within
this project item</returns>
public CodeClass2 FindClassDefinition ( ProjectItem n ) {
CodeClass2 result = null;
if ( ( n == null ) ||
( n.Kind !=
Constants.vsProjectItemKindPhysicalFile ) ) {
return result;
}
// checking every code element in the project items
FileCodeModel
foreach ( object o in n.FileCodeModel.CodeElements ) {
if ( o as CodeElement2 != null ) {
// if it's of kind vsCMElementClass, we are done
if ( ( o as CodeElement2 ).Kind ==
vsCMElement.vsCMElementClass ) {
result = ( o as CodeClass2 );
break;
}
else if ( ( o as CodeElement2 ).Kind ==
vsCMElement.vsCMElementNamespace ) {
// if it's of kind vsCMElementNamespace than
we have to iterate the children of the namespace
foreach ( object c in ( o as
CodeElement2 ).Children ) {
if ( ( c as CodeElement2 != null ) &&
( ( c as CodeElement2 ).Kind ==
vsCMElement.vsCMElementClass ) ) {
result = ( c as CodeClass2 );
break;
}
}
if ( result != null ) {
break;
}
}
}
}
return result;
}
If you found the CodeClass you can easily use the methods:
AddProperty, AddFunction etc. pp. to create method stubs. (contact me
if you need further guidance).
cu
andi