Question

I am creating code generation tool (auto code generation based on table structure) as a Windows forms application in Visual Studio 2012 using .NET Framework 4.0. It's generating the portable object, controller, WCF services and business logic code files.

All code files bundle in the appropriate project and all project bundle in one solution. The solution and projects need to create dynamically through program.

I have tried to create the solution and project using Visual Studio Add-in project. It is working fine in Add-In project (separate solution). The OnConnection method call automatically in Add-in project. Now I want to implements this in my code generation tool. While debugging in Add-In project the application variable shown like COM object.

I am tried to pass the value for OnConnection method from code generation tool, it throws an error (I passed this object for application variable). I really don't know how to call this method from my code generation tool. Anyone help this?

Code

 private DTE2 _applicationObject;
 private AddIn _addInInstance;

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    createProjectsFromTemplates(_applicationObject);
}


public void createProjectsFromTemplates(DTE2 dte)
{
    try
    {
        Solution2 soln = (Solution2)dte.Solution;
        string csTemplatePath;
        string csPrjPath = "SamplePath\\TestCreateProject";
        csTemplatePath = soln.GetProjectTemplate("WpfApplication.zip", "CSharp");
        System.Windows.Forms.MessageBox.Show("C# template path: " + csTemplatePath);

        soln.AddFromTemplate(csTemplatePath, csPrjPath, "NewWCFCSharpAutoGeneratorProject", false);

        Project prj;
        ProjectItem prjItem;

        String itemPath;
        // Point to the first project (the Visual Basic project).
        prj = soln.Projects.Item(1);

        prjItem = prj.ProjectItems.AddFromFileCopy("SampelCSharp.cs");
    }
    catch (System.Exception ex)
    {
        System.Windows.Forms.MessageBox.Show("ERROR: " + ex.Message);
    }
}
Was it helpful?

OTHER TIPS

You can instantiate a VS from the host application and generate the files. Hope that will work. The below code works well for me.

Use the following namespaces to get work the below given code.

Namespaces:

using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
using System.Resources;
using System.Reflection;

Code:

EnvDTE80.DTE2 dte2;
dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.11.0");

Connect objConnect = new Connect();
Array objArray = null;
objConnect.OnConnection(dte2, ext_ConnectMode.ext_cm_UISetup, null, ref objArray);

You can use this. This is for .cs project files and framewwork above .NET 2.0 versions. VB project sources are not compatible.

protected void Build(string project)
    {


        Engine engine = new Engine();

        BuildPropertyGroup properties = new BuildPropertyGroup();

        properties.SetProperty(@"Configuration", @"Debug");


        // Point to the path that contains the .NET Framework 2.0 CLR and tools
        engine.BinPath = @"c:\windows\microsoft.net\framework\v3.5";

        // Instantiate a new FileLogger to generate build log
        FileLogger logger = new FileLogger();

        // Set the logfile parameter to indicate the log destination
        string str   = @"logfile=D:\temp";
               str  += project.Substring(project.LastIndexOf("\\"), project.LastIndexOf(".") - project.LastIndexOf("\\")) + ".log";
        logger.Parameters = str;

        // Register the logger with the engine
        engine.RegisterLogger(logger);


        // Build a project file
        bool success = engine.BuildProjectFile(project, new string[] { "build" }, properties);

        //Unregister all loggers to close the log file
        engine.UnregisterAllLoggers();

        using (BinaryWriter writer = new BinaryWriter(File.Open(@"D:\temp\Prj.log", FileMode.Append)))
        {
            if (success)
            {
                writer.Write("\nBuild Success :" + project.Substring(project.LastIndexOf("\\")));
            }
            else
            {
                writer.Write("\nBuild Fail :" + project.Substring(project.LastIndexOf("\\")));
            }
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top