簡體   English   中英

動態創建解決方案文件(.sln)和項目文件(.csproj)

[英]Create solution file(.sln) and project files(.csproj) dynamically

我正在使用.NET Framework 4.0在Visual Studio 2012中創建代碼生成工具(基於表結構的自動代碼生成)作為Windows窗體應用程序。 它正在生成可移植對象,控制器,WCF服務和業務邏輯代碼文件。

所有代碼文件都捆綁在適當的項目中,而所有項目捆綁在一個解決方案中。 解決方案和項目需要通過程序動態創建。

我試圖使用Visual Studio加載項項目創建解決方案和項目。 在外接程序項目(單獨的解決方案)中,它運行良好。 Add-in項目中自動調用OnConnection方法。 現在,我想在我的代碼生成工具中實現這一點。 Add-In application項目中調試時, application變量顯示為COM object

我試圖從代碼生成工具傳遞OnConnection方法的值,它引發錯誤(我this對象傳遞給application變量)。 我真的不知道如何從我的代碼生成工具中調用此方法。 有人幫忙嗎?

 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);
    }
}

您可以從主機應用程序實例化VS並生成文件。 希望能奏效。 以下代碼對我來說效果很好。

使用以下名稱空間可以使用以下給定的代碼進行工作。

命名空間:

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

碼:

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);

您可以使用它。 適用於.NET 2.0以上版本的.cs項目文件和框架。 VB項目源不兼容。

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("\\")));
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM