1

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

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

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

我試圖從代碼生成工具傳遞OnConnection方法的值,它會拋出一個錯誤(我通過對象爲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); 
    } 
} 
+0

我認爲你的問題是重複的,除了應用程序類型的差異,你想調用加載項。我在那裏留言。 [在另一個應用程序中使用Addin項目](http://stackoverflow.com/questions/21986644/use-addin-project-in-another-application)。 – Ursegor

+0

不是專門和回答這個問題,但可能是有用的:http://stackoverflow.com/questions/414309/generate-solution-file-from-list-of-csproj –

回答

1

您可以從主機應用程序實例化一個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); 
0

您可以使用此。這是.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("\\"))); 
      } 
     } 
    } 
相關問題