Visual Studio Development Bookmark and Share   
 index > Visual Studio Guidance Automation Toolkit > Some doubts about EnvDTE
 

Some doubts about EnvDTE

Hi, i'm trying to add methods in existing classes of my solution. I can't control the outcome of the AddFunction method,

AddFunction("MethodName", vsCMFunction.vsCMFunctionFunction, "List<MyType>", -1, vsCMAccess.vsCMAccessPublic, null);

Is there any way to define a static class with a return type?(public static List<MyType> MethodName())

Is there any way to define a return type but do not generate default return MyType sentence?

Thanks

Martin

Mar77incho  Tuesday, September 11, 2007 11:05 PM
As this question is really related to pure VS automation (and not GAX) you should use the VS extensibility forums right here in MSDN forums next time for pure DTE and VSIP questions. There you may even find a previous similar question to this one.

thanks,
-Victor.
vga  Thursday, September 13, 2007 3:21 PM
Hi Martin, you should use the vsCMFunctionShared vsCMFunction enum value. In VB shared in the same as static in C#.

HTH,
-Adrian
Adrian Alonso  Tuesday, September 11, 2007 11:41 PM
Hi Adrian, i´ve tried that and it throws an exception : System.NotImplementedException: Not implemented
en EnvDTE.CodeClass.AddFunction(String Name, vsCMFunction Kind, Object Type, Object Position, vsCMAccess Access, Object Location)

Thanks,

Martin.


Mar77incho  Wednesday, September 12, 2007 3:06 AM
Are you using the ProjectItem's FileCodeModel property to add the function to the target class? Because the FileCodeModel::AddFunction(...) receives 5 parameters: name, kind, type, position and access

[DispId(10)]
CodeFunction AddFunction(string Name, vsCMFunction Kind, object Type, object Position, vsCMAccess Access);

-Adrian
Adrian Alonso  Wednesday, September 12, 2007 3:32 AM
I´ve tried that and i got the exception: System.Runtime.InteropServices.COMException (0x80004005): Unspecified error

Here´s the original code:

private void buscarClase()
{

TextWriter tw = new StreamWriter("C:\\Documents and Settings\\ArchivoPlanoFacade.txt");
try
{
DTE vs = (DTE)GetService(typeof(DTE));
String nombre = targetFileName;
//Obtengo los elementos del proyecto "
foreach (ProjectItem item in project.ProjectItems)
{
if (item.Name.ToUpper().Equals(nombre.ToUpper()))
{
FileCodeModel model = (FileCodeModel)item.FileCodeModel;
//model.AddFunction(nombremetodoGet, vsCMFunction.vsCMFunctionShared, tipoRetornoGet, -1, vsCMAccess.vsCMAccessPublic);
foreach (CodeElement codeElement in model.CodeElements)
{
if (codeElement is CodeNamespace)
{
CodeNamespace codigo = ((CodeNamespace)codeElement);
for (int i = 0; i < codigo.Members.Count; i++)
{
tw.Write(codigo.Members.Item(i + 1).Name);


CodeClass codeClass = (CodeClass)codigo.Members.Item(i+1);
//Estoy en la clase Business Facade
// Agrego el Metodo GetModule()
string nombremetodoGet = "Get" + className + suf;
string tipoRetornoGet = "List<" + ClassName + ">";
CodeFunction codeFunctionGet = (CodeFunction)codeClass.AddFunction(nombremetodoGet, vsCMFunction.vsCMFunctionFunction, tipoRetornoGet, -1, vsCMAccess.vsCMAccessPublic, null);
EditPoint2 editPointGet = (EditPoint2)codeFunctionGet.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
editPointGet.Indent(null, 0);
editPointGet.Delete(50);
editPointGet.Insert("return DAOFactory." + ClassName + suf + "DAO.Get" + ClassName + suf + "();");
editPointGet.InsertNewLine(1);
editPointGet.Insert("}");
editPointGet.Indent(null, 2);

// Agrego el Metodo Add
string nombreMetodoAdd = "Add" + className;
CodeFunction codeFunctionAdd = (CodeFunction)codeClass.AddFunction(nombreMetodoAdd, vsCMFunction.vsCMFunctionFunction, "void", -1, vsCMAccess.vsCMAccessPublic, null);
codeFunctionAdd.AddParameter(classNameMin, className, -1);
EditPoint2 editPointAdd = (EditPoint2)codeFunctionAdd.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
editPointAdd.Indent(null, 0);
editPointAdd.Insert("IAuthorizationProvider ruleProvider = AuthorizationFactory.GetAuthorizationProvider();");
editPointAdd.InsertNewLine(2);
editPointAdd.Insert("IPrincipal principal = Usuario.CurrentPrincipal;");
editPointAdd.Indent(null, 2);
editPointAdd.InsertNewLine(1);
editPointAdd.Insert("if (!ruleProvider.Authorize(principal, " + '"' + className + "Add" + '"' + ")) {");
editPointAdd.Indent(null, 2);
editPointAdd.InsertNewLine(1);
editPointAdd.Insert("throw new UnauthorizedAccessException("+'"'+"El usuario no tiene permiso para agregar "+classNameMin+suf+'"'+");");
editPointAdd.Indent(null, 3);
editPointAdd.InsertNewLine(1);
editPointAdd.Insert("}");
editPointAdd.Indent(null, 2);
editPointAdd.InsertNewLine(1);
editPointAdd.Insert("DAOFactory."+className+suf+"DAO.Add"+className+"("+classNameMin+");");
editPointAdd.Indent(null, 2);
editPointAdd.InsertNewLine(1);
editPointAdd.Insert("}");
editPointAdd.Indent(null, 2);
editPointAdd.InsertNewLine(1);

// Agrego el Metodo Update
string nombreMetodoUpdate = "Update" + className;
CodeFunction codeFunctionUpdate = (CodeFunction)codeClass.AddFunction(nombreMetodoUpdate, vsCMFunction.vsCMFunctionFunction, "void", -1, vsCMAccess.vsCMAccessPublic, null);
codeFunctionUpdate.AddParameter(classNameMin, className, -1);
EditPoint2 editPointUpdate = (EditPoint2)codeFunctionUpdate.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
editPointUpdate.Indent(null, 2);
editPointUpdate.Insert("IAuthorizationProvider ruleProvider = AuthorizationFactory.GetAuthorizationProvider();");
editPointUpdate.InsertNewLine(2);
editPointUpdate.Insert("IPrincipal principal = Usuario.CurrentPrincipal;");
editPointUpdate.Indent(null, 2);
editPointUpdate.InsertNewLine(1);
editPointUpdate.Insert("if (!ruleProvider.Authorize(principal, " + '"' + className + "Update" + '"' + ")) {");
editPointUpdate.Indent(null, 2);
editPointUpdate.InsertNewLine(1);
editPointUpdate.Insert("throw new UnauthorizedAccessException(" + '"' + "El usuario no tiene permiso para modificar " + classNameMin + suf + '"' + ");");
editPointUpdate.Indent(null, 3);
editPointUpdate.InsertNewLine(1);
editPointUpdate.Insert("}");
editPointUpdate.Indent(null, 2);
editPointUpdate.InsertNewLine(1);
editPointUpdate.Insert("DAOFactory." + className + suf + "DAO.Update" + className + "(" + classNameMin + ");");
editPointUpdate.Indent(null, 2);
editPointUpdate.InsertNewLine(1);
editPointUpdate.Insert("}");
editPointUpdate.Indent(null, 2);
editPointUpdate.InsertNewLine(1);

// Agrego el Metodo Update
string nombreMetodoDelete = "Delete" + className;
CodeFunction codeFunctionDelete = (CodeFunction)codeClass.AddFunction(nombreMetodoDelete, vsCMFunction.vsCMFunctionFunction, "void", -1, vsCMAccess.vsCMAccessPublic, null);
codeFunctionDelete.AddParameter(classNameMin+"Id","int", -1);
EditPoint2 editPointDelete = (EditPoint2)codeFunctionDelete.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
editPointDelete.Indent(null, 2);
editPointDelete.Insert("IAuthorizationProvider ruleProvider = AuthorizationFactory.GetAuthorizationProvider();");
editPointDelete.InsertNewLine(2);
editPointDelete.Insert("IPrincipal principal = Usuario.CurrentPrincipal;");
editPointDelete.Indent(null, 2);
editPointDelete.InsertNewLine(1);
editPointDelete.Insert("if (!ruleProvider.Authorize(principal, " + '"' + className + "Delete" + '"' + ")) {");
editPointDelete.Indent(null, 2);
editPointDelete.InsertNewLine(1);
editPointDelete.Insert("throw new UnauthorizedAccessException(" + '"' + "El usuario no tiene permiso para borrar " + classNameMin + suf + '"' + ");");
editPointDelete.Indent(null, 3);
editPointDelete.InsertNewLine(1);
editPointDelete.Insert("}");
editPointDelete.Indent(null, 2);
editPointDelete.InsertNewLine(1);
editPointDelete.Insert("DAOFactory." + className + suf + "DAO.Delete" + className + "(" + classNameMin + ");");
editPointDelete.Indent(null, 2);

}
}

}

}
}
}
catch (Exception ex)
{
tw.WriteLine("EXCEPCION " + ex.ToString());
}
tw.Close();
}


Thanks,

Martín
Mar77incho  Wednesday, September 12, 2007 4:34 AM
As this question is really related to pure VS automation (and not GAX) you should use the VS extensibility forums right here in MSDN forums next time for pure DTE and VSIP questions. There you may even find a previous similar question to this one.

thanks,
-Victor.
vga  Thursday, September 13, 2007 3:21 PM

Hello,

i used this method to make my function static. anyone have a better method?

CodeFunction UtilFunction = UtilClass.AddFunction("GetxxxByyyy", vsCMFunction.vsCMFunctionFunction, "MYType", -1, vsCMAccess.vsCMAccessPublic, null);

UtilEditPoint = UtilFunction.GetStartPoint(vsCMPart.vsCMPartHeader).CreateEditPoint();

UtilEditPoint.ReplaceText(6, "public static", 0);

Fethi Gürcan  Wednesday, January 02, 2008 8:09 AM

You can use google to search for other answers

Custom Search

More Threads

• Custom UnfoldTemplate
• Smart Client Software Factory (Recipe Framework Error) during Enable and Disable Packages
• How should I deploy additional binaries?
• Recipe in Context menus
• Save the solution on a specific location
• What makes guidance package show up in "New Project" dialog box?
• GAX Templates do not load
• Unable to uninstall several guidance packages
• CodeDom -Error when Run Recipe
• Reference Issue