C# Visual Studio项模板:替换为项目名称
我正在尝试在Visual Studio 2013中为EF代码第一个上下文类创建一个项模板。除了我想要一个指向当前项目中的名称空间的using语句之外,我所有的工作都正常。例如,在ProjectName.Contexts中创建模板项的实例时,using语句应为: 使用ProjectName.Models 问题是我找不到一种方法来替换项目模板中的零件。据我所知,$projectname$和$safeprojectname$只在项目模板中工作,而不在项目模板中工作 我的模板如下:C# Visual Studio项模板:替换为项目名称,c#,visual-studio,templates,C#,Visual Studio,Templates,我正在尝试在Visual Studio 2013中为EF代码第一个上下文类创建一个项模板。除了我想要一个指向当前项目中的名称空间的using语句之外,我所有的工作都正常。例如,在ProjectName.Contexts中创建模板项的实例时,using语句应为: 使用ProjectName.Models 问题是我找不到一种方法来替换项目模板中的零件。据我所知,$projectname$和$safeprojectname$只在项目模板中工作,而不在项目模板中工作 我的模板如下: using Syst
using System;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using $safeprojectname$.DAL.Models; // This is the problem line
namespace $rootnamespace$
{
public class $safeitemrootname$
: DbContext
{
// Constructors =======================================================
public $safeitemrootname$()
: base("$safeitemrootname$")
{
}
// DBSets =============================================================
// public DbSet<Company> Companies { get; set; }
// Configuration ======================================================
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
}
正如您所看到的,$projectname$substitution只是被忽略了
任何帮助都将不胜感激
Jason如以下MSDN链接所述: 只有在“新建项目”对话框中提到项目名称时,项目名称才会出现。所以,如果您在现有项目中使用它,它将不起作用。请参见下面的屏幕截图:
我找到了一个解决方法-使用自定义模板向导,并在源代码管理资源管理器中检索当前选定的项 由于在解决方案树中选择项目节点时,自定义项模板仅在“添加新”菜单中可用,因此可以确定项目名称(以及可供EnvDTE使用的所有其他信息)并将其添加到替换目录中
using EnvDTE80;
public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
{
//Adding by template is unavailable in the context menu when selecting more than one item -> use index 0.
UIHierarchyItem selectedUiHierarchyItem = (UIHierarchyItem )dte.ToolWindows.SolutionExplorer.SelectedItems[0];
//not null when selecting solution node (when adding a project)
Solution selectedSolution = selectedUiHierarchyItem.Object as Solution
//not null when selecting project node (when adding an item) or when selecting a solution folder (when adding a project)
Project selectedProject = selectedUiHierarchyItem.Object as Project;
//Determine which kind of project node:
if (selectedProject != null)
{
if (selectedProject.Kind == EnvDTE.Constants.vsProjectKindSolutionItems)
{
//solution folder
}
else
{
//project
replacementsDictionary.Add("$projectname$", selectedProject.Name);
}
}
}
使用EnvDTE80;
public void RunStarted(对象自动对象、字典替换Dictionary、向导runKind runKind、对象[]customParams)
{
//选择多个项目->使用索引0时,上下文菜单中的“按模板添加”不可用。
UIHierarchyItem selectedUiHierarchyItem=(UIHierarchyItem)dte.ToolWindows.SolutionExplorer.SelectedItems[0];
//选择解决方案节点时不为空(添加项目时)
Solution selectedSolution=SelectedUhierarchyItem.Object作为解决方案
//选择项目节点(添加项目时)或选择解决方案文件夹(添加项目时)时不为null
Project selectedProject=SelectedUhierarchyItem.Object作为项目;
//确定哪种类型的项目节点:
如果(selectedProject!=null)
{
if(selectedProject.Kind==EnvDTE.Constants.vsProjectKindSolutionItems)
{
//解决方案文件夹
}
其他的
{
//计划
replacementsDictionary.Add(“$projectname$”,selectedProject.Name);
}
}
}
在Root.vstemplate
中添加copyParameters=“true”
,如下所示
ProjectTemplateLink ProjectName="Domain.$projectname$" CopyParameters="true">
在代码中使用$ext\u projectname$
解析项目名称
using Provider.$ext_projectname$
我的同事在工作中能够找到一个参数来修复它,该参数检测当前的项目名称
$rootnamespace$
不,这在项目模板中也可以正常工作。它将替换为项目的默认名称空间值。正如在Project+属性的应用程序选项卡中设置的。这为我指明了正确的方向。请注意,使用的“dte”变量是对DTE2的automationObject强制转换,因此您需要一个对EnvDTE80Thank Pedro的引用-我在顶部添加了一个using,以使其更清晰。这非常有效,与名称所示不同,它将文件添加到的文件夹的完整命名空间放在一起。这很有效(VS2017)基于zip文件的多项目模板,但ProjectName=“Domain.$ProjectName$”也是项目文件夹的名称以及域.$ProjectName$.csproj文件的名称,项目自己的vstemplate文件的TargetFileName=“MyNewName.csproj”无效。
$rootnamespace$