我想要一个模板,它以某种方式接受class_name
属性并创建一个类,其名称是class_name
的值
理想情况下,我应该能够在包含public partial class MyClass
定义的MyClass.cs
中执行我的模板,并且将class_name
设置为MyClass
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
public partial class <#=class_name #>
{
}
产生错误:
Error 1 Compiling transformation: The name 'class_name' does not exist in the current context
我如何将class_name
的定义放入其中?
你可以这样定义class_name:
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
<# var class_name = "MyClass"; #>
public partial class <#=class_name #>
{
}