将一些代码行插入其他项目

时间:2011-03-14 11:18:11

标签: c# .net vb.net

我有一个项目(我们称之为project1),它将向另一个项目的类添加属性。事情是我想自动制作它。所以在我的项目中有两行代码,我想在class.bb或class.cs的底部添加一个名为project2的项目。可能吗?如果是这样,怎么样? 它是一个数据访问构建器软件,在向我们的数据库添加一些新列之后,我们使用它来生成其属性及其参数以在BLL中添加。由于我们经常这样做(改变我们的数据库结构),我不想从数据访问构建器软件复制数据并手动将其添加到project2。就是这样!

3 个答案:

答案 0 :(得分: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 :(得分:1)

与部分类组合的链接文件看起来像是您的问题的解决方案。使您的数据构建器软件发出如下代码:

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

并在你的项目中创建这样的类:

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

在解决方案中包含生成的文件(Add-&gt; Existing Item-&gt; Add As Link),编译器会将这2个文件的内容合并为单个类定义。

http://msdn.microsoft.com/en-us/library/wa80x488.aspx中查看有关部分课程的更多信息。编译器甚至可以自动删除未实现的方法定义以及调用。 AFAIK此功能专门用于处理生成的源。

答案 2 :(得分:0)

您可以拥有链接文件。

一个项目有原始文件,第二个项目只有该文件的链接。 在此查看详细信息:MSDN