2011-03-14 50 views
0

我有一個項目(我們稱之爲project1),它將把屬性添加到另一個項目的類中。事情是我想自動製作。所以在我的項目中有兩行代碼,我想在class.vb或class.cs的底部添加一個名爲project2的項目。可能嗎?如果是這樣,怎麼樣? 它是一個數據訪問構建器軟件,在向我們的數據庫添加一些新列後,我們使用它來生成其屬性和參數,並將其添加到BLL中。由於我們經常這樣做(改變我們的數據庫結構),我不想從數據訪問構建器軟件複製數據並手動將其添加到project2。就是這樣了!將一些代碼行插入其他項目

+0

目前還不清楚你要求的是:你想複製/粘貼源代碼?重用代碼?運行時訪問? – 2011-03-14 11:20:51

+0

有關這兩條線是什麼以及爲什麼您希望它們到處的更多信息將有所幫助。事實上,這個問題太模糊了,沒有猜測就回答。 – 2011-03-14 11:24:55

+0

我編輯了我的問題球員。現在有點更清楚了嗎? – aaaaaaaaaaaa 2011-03-14 11:28:08

回答

0

您可以鏈接文件。

一個項目有原始文件,第二個項目只有一個指向該文件的鏈接。 看看這裏的細節:MSDN

2

如果你真想勾搭自動將您的解決方案中,你可以做到這一點,通過結合

我把它用自動添加一堆自動生成的資源文件。我將代碼稍微剝離了一點,所以如果它不起作用,只需取消註釋Debugger.Launch()行並在新的VS實例中調試代碼。

<#@ template inherits="Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation" language="C#v3.5" debug="true" hostSpecific="true" #> 
<#@ output extension=".txt" #> 
<#@ Assembly Name="System.dll" #> 
<#@ Assembly Name="System.Core.dll" #> 
<#@ Assembly Name="System.Design.dll" #> 
<#@ Assembly Name="System.Drawing.dll" #> 
<#@ Assembly Name="System.Windows.Forms.dll" #> 
<#@ Assembly Name="envdte.dll" #> 
<#@ import namespace="System" #> 
<#@ import namespace="System.CodeDom.Compiler" #> 
<#@ import namespace="System.Collections.Generic" #> 
<#@ import namespace="System.Drawing" #> 
<#@ import namespace="System.IO" #> 
<#@ import namespace="System.Linq" #> 
<#@ import namespace="System.Resources" #> 
<#@ import namespace="System.Resources.Tools" #> 
<#@ import namespace="EnvDTE" #> 
<#@ import namespace="Microsoft.CSharp" #> 

<# 
    var rootPath = Path.GetDirectoryName(this.Host.TemplateFile); 
    var resourcesPath = Path.Combine(rootPath, "Resources"); 
#> 

This template file doesn't create any usable source file. 

The resource files where added at: <#= System.DateTime.Now.ToShortDateString() #> <#= DateTime.Now.ToLongTimeString() #> 
<# 

    //System.Diagnostics.Debugger.Launch(); 

    EnvDTE.DTE dte = (EnvDTE.DTE)((IServiceProvider)this.Host) 
         .GetService(typeof(EnvDTE.DTE)); 

    EnvDTE.Projects projects = dte.Solution.Projects; 
    EnvDTE.Project iconProject = projects.Cast<EnvDTE.Project>().Where(p => p.Name == "Icons").Single(); 
    EnvDTE.ProjectItem resourcesFolder = iconProject.ProjectItems.Cast<EnvDTE.ProjectItem>().Where(item => item.Name == "Resources").Single(); 

    // Delete all existing .resx files within the project 
    foreach (var item in resourcesFolder.ProjectItems.Cast<EnvDTE.ProjectItem>()) 
    { 
     item.Delete(); 
    } 

    // Iterate over all files on the disk 
    foreach (var resourceFile in Directory.GetFiles(resourcesPath, "*.resx")) 
    { 
     // Add them to the project ... 
     var createdItem = resourcesFolder.Collection.AddFromFile(resourceFile); 
     // and mess a little bit around in the properties. 
     createdItem.Properties.Item("CustomTool").Value = "ResXFileCodeGenerator"; 
    } 
#> 
1

鏈接的文件與部分類的組合看起來像解決您的問題。讓您的數據生成器軟件發出這樣的代碼:

public partial class MyClass 
{ 
    public void SomeFunction1() 
    { 
    } 
} 

,並在項目中創建的類是這樣的:

public partial class MyClass 
{ 
    public void MyOtherFunction() 
    { 
    } 
} 

包括在溶液中生成的文件(添加 - >現有用品 - >添加鏈接)編譯器會將這兩個文件的內容合併爲一個類定義。

查看關於部分班級的更多信息:http://msdn.microsoft.com/en-us/library/wa80x488.aspx。編譯器甚至可以自動刪除未實現的方法定義以及調用。 AFAIK此功能是專門爲使用生成的來源而設計的。

相關問題