Add-Ins (CodeModel-C#)
Messages   Related Types
This message was discovered on microsoft.public.vsnet.documentation.

Post a new message to this list...

CyberBizSoft Support
Hello all,

I am looking for some samples on using the IDE Extensibility's CodeElements.

I am building some add-ins and I have a couple of problems:

1 - Adding a constructor to a class using:

EnvDTE.CodeFunction cF =
codeClss.AddFunction("Account",EnvDTE.vsCMFunction.vsCMFunctionConstructor,0
,-1,EnvDTE.vsCMAccess.vsCMAccessPublic,"");

This gives me the error: Invalid parameter! But it does not say which one:)

2 - Also I am able to create properties using:
EnvDTE.CodeProperty codeProp =
codeClss.AddProperty("Name","Name","string",-1,EnvDTE.vsCMAccess.vsCMAccessP
ublic, "");

but I can not modify the getter or the setter after that.

I will appreciate any help in getting some sample code on manipulating these
CodeElements.

Regards,

Santos

Reply to this message...
 
    
Seth Grossman [MSFT]
Santos-

We are currently investigating this issue, and will post our findings as
soon as possible.

Thank you for your patience,
Seth Grossman
Microsoft Corp.
This posting is provided "AS IS" with no warranties, and confers no rights.

"CyberBizSoft Support" <Click here to reveal e-mail address> wrote in message
news:e0pdu66uBHA.2760@tkmsftngp05...
[Original message clipped]

Reply to this message...
 
    
Seth Grossman [MSFT]
Santos-

Ok, so you had two issues:

-an invalid parameter for the constructor you were adding

and

- modifying the getter and setter for the property you created.

For the constructor issue:

Currently, there is an issue in extensibility with creating constructors in
C#; we are addressing it now...

In the meantime, there are two approaches to creating a constructor:

Solution 1
The first solution is to insert a string literal into the class, using the
EditPoint object. Here's a bit of macro code that creates a class in the
code file "newfile.cs" and adds a constructor. You may notice that this is
VB code - the other solution we provided is in C#, but takes a different
approach from the design you were using.

Public Sub AddAConstructor()

Dim proj As Project

proj = DTE.Solution.Projects.Item(1)

Dim pi As ProjectItem

pi = proj.ProjectItems.Item("newfile.cs")

Dim games As CodeNamespace

Dim name As String = InputBox("namespace")

games = pi.FileCodeModel.AddNamespace(name)

' Add a class to the namespace.

Dim chess As CodeClass

chess = games.AddClass("Chess")

Dim ep As EditPoint = _
chess.GetEndPoint( _
EnvDTE.vsCMPart.vsCMPartBody).CreateEditPoint()

ep.LineUp(1)

Dim indent As String = ControlChars.CrLf & ControlChars.Tab & _
ControlChars.Tab

Dim constructor As String = _

indent & "Chess()" & _

indent & "{" & _

indent & ControlChars.Tab & "int i = 10;" & _

indent & ControlChars.Tab & _
"MessageBox.Show(i.ToString());" & _

indent & "}" & ControlChars.CrLf

ep.Insert(constructor)

End Sub

Solution 2
The second solution uses the System.CodeDom namespace to generate code. This
bit of C# code creates a class and writes the class to the console.

using System;

using System.IO;

using System.CodeDom;

using System.CodeDom.Compiler;

namespace ConstructorTest1

{

/// <summary>

/// Summary description for Class1.

/// </summary>

class Class1

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main(string[] args)

{

CodeDomProvider csharpProv;

ICodeGenerator csharpGenerator;

StringWriter s = new StringWriter();

CodeCompileUnit ccUnit = new
CodeCompileUnit();

csharpProv = new
Microsoft.CSharp.CSharpCodeProvider();

csharpGenerator =
csharpProv.CreateGenerator(s);

CodeNamespace codens = new
CodeNamespace();

CodeGeneratorOptions cgOpt = new
CodeGeneratorOptions();

codens.Name = "Games";

ccUnit.Namespaces.Add(codens);

CodeTypeDeclaration class1 = new
CodeTypeDeclaration();

class1.Name = "Chess";

class1.IsClass = true;

codens.Types.Add(class1);

CodeConstructor cctor = new
CodeConstructor();

cctor.Attributes =
MemberAttributes.Public;

cctor.Statements.Add(

new CodeSnippetStatement("for(int i=0; i<3; i++);"));

class1.Members.Add(cctor);

csharpGenerator.GenerateCodeFromCompileUnit(

ccUnit, s, cgOpt);

Console.WriteLine(s);

}

}

}

For the getter and the setter issue, we were unable to figure out exactly
what you were trying to accomplish - what do you want to change about the
getter and setter?

If you need further clarification of the above, or have any other questions,
please holler.

Thanks
Seth Grossman (and Robin Reynolds-Haertle)
Microsoft Corp.
This posting is provided "AS IS" with no warranties, and confers no rights.

"Seth Grossman [MSFT]" <Click here to reveal e-mail address> wrote in message
news:3c7fe823$Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
 
EnvDTE.CodeFunction
EnvDTE.CodeProperty
EnvDTE.vsCMAccess
EnvDTE.vsCMFunction
EnvDTE.vsCMPart
Microsoft.CSharp.CSharpCodeProvider
System.CodeDom.CodeCompileUnit
System.CodeDom.CodeConstructor
System.CodeDom.CodeNamespace
System.CodeDom.CodeSnippetStatement
System.CodeDom.CodeTypeDeclaration
System.CodeDom.Compiler.CodeDomProvider
System.CodeDom.Compiler.CodeGeneratorOptions
System.CodeDom.Compiler.ICodeGenerator
System.CodeDom.MemberAttributes
System.Console
System.IO.StringWriter
System.Windows.Forms.MessageBox




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com