C#项目模板和项目名称的变量?
我的大部分代码都包含三个相关的类,一个基于一个名为IImplementation的接口,一个Execute方法(以帮助强制执行单一责任)以及一个响应和上下文类。 我厌倦了这个,它费力,重复和耗时,所以今天我决定写一个C#项目模板。
模板文件和.vstemplate文件如下所示:
Implmentation.vstemplate
Implementation Genertates a skeletal concrete IImplementation class complete with Response and Context classes. Class.ico 74206442-81cb-4328-afc8-08c40fb5d3e7 CSharp 2.0 1 Class.cs System Implementation.cs Response.cs Context.cs Microsoft.VisualStudio.Web.Application, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a Microsoft.VisualStudio.Web.Application.WATemplateWizard />
Implementation.cs
using System; using $safeprojectname$.Implementation.Attibutes; using $safeprojectname$.Implementation.Interfaces; namespace $rootnamespace$ { [AutofacRegisterImplementation] public class $classname$ : IImplementation { #region Execute public $fileinputname$Response Execute($fileinputname$Context context) { throw new NotImplementedException(); } #endregion } }
Response.cs
namespace $rootnamespace$ { public class $classname$Response { #region Properties #endregion } }
Context.cs
namespace $rootnamespace$ { public class $classname$Context { #region Properties #endregion } }
它有点好用,我很高兴,因为我甚至设法将响应和上下文类嵌套在植入类下面,这意味着项目文件中包含大量实现的混乱(我很难避免这样做!)
除了来自Implementation.cs的以下片段:
using $safeprojectname$.Implementation.Attibutes; using $safeprojectname$.Implementation.Interfaces;
生成此(整个生成的文件):
using System; using $safeprojectname$.Implementation.Attibutes; using $safeprojectname$.Implementation.Interfaces; namespace Balcony.Common.Implementation { [AutofacRegisterImplementation] public class Test1 : IImplementation { #region Execute public Test1Response Execute(Test1Context context) { throw new NotImplementedException(); } #endregion } }
这让我得出结论$ safeprojectname $仅适用于C#项目模板。 这是正确的吗?
有人知道如何在C#项模板中正确扩展安全的项目名称变量吗?
同时在它…如何进入New from Template子菜单? 你在哪里可以窃取Class.ico文件? 🙂
我会说使用$ rootnamespace $代替项目名称,这应该做同样的事情。