Create an extension for visual studio that adds classes dynamically

Asked

Viewed 166 times

1

I am creating an extension for the visual studio that aims to create a project based on the options that the programmer wants. In this case a Wizard will be created where it will choose the entity(s) it wants to create, and based on this selection will be created the respecivas classes.

I already created the template (in my empty case) and respective Wizard for the selection of entities. The entity selection Wizard that is displayed when the user creates the project is in the image below. The goal is that when doing OK a project is created with the classes he chose, for example "Consolidacaosaldos" should give rise to a class within an "Accounting" folder"

Doubt is how to create classes dynamically based on selection.

inserir a descrição da imagem aqui

namespace Primavera.Extensibility.Wizard
{
    public class WizardImplementation : IWizard
    {
        private Modules frm;
        private string customMessage;

        // This method is called before opening any item that   
        // has the OpenInEditor attribute.  
        public void BeforeOpeningFile(ProjectItem projectItem)
        {
        }

        public void ProjectFinishedGenerating(Project project)
        {
        }

        // This method is only called for item templates,  
        // not for project templates.  
        public void ProjectItemFinishedGenerating(ProjectItem
            projectItem)
        {
        }

        // This method is called after the project is created.  
        public void RunFinished()
        {
        }

        public void RunStarted(object automationObject,
            Dictionary<string, string> replacementsDictionary,
            WizardRunKind runKind, object[] customParams)
        {
            try
            {
                // Display a form to the user. The form collects the classes checked 
                frm = new Modules();
                frm.ShowDialog();

                customMessage = Modules.CustomMessage;


                replacementsDictionary.Add("$custommessage$",
                    customMessage);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        // This method is only called for item templates,  
        // not for project templates.  
        public bool ShouldAddProjectItem(string filePath)
        {
            return true;
        }
    }
}
  • Congratulations on doing something above average, I wanted people to be creative in doing things to make their lives easier. I don’t even know if this is the case, if I wouldn’t have a better solution, but anyway, it’s cool, even if there’s one thing I don’t like in the code. All you had to do was ask what the doubt is.

  • @Maniero already reworked the question. The question is to dynamically create the classes/folders in the project based on the selection.

1 answer

2


This is simple to do.

You need to create an item template with the class template you want to add to your project and then just cycle over the collection of objects you have.

EnvDTE80.Solution2 solution = project.DTE.Solution as EnvDTE80.Solution2;
string itemPath = solution.GetProjectItemTemplate("Class.zip", "CSharp");

string[] namespaceParts = type.Namespace.Split('.');
if (namespaceParts.Length == 4)
{
    string module = namespaceParts[2];
    string moduleType = namespaceParts[3];
    string className = type.Name;     

    ProjectItem rootFolder = project.ProjectItems.Cast<ProjectItem>().FirstOrDefault(i => i.Name == module) ?? project.ProjectItems.AddFolder(module);
    ProjectItem itemEditors = rootFolder.ProjectItems.AddFromTemplate(itemPath, className + ".cs");
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.