Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# T4。错误:表达式块计算为空_C#_.net_Code Generation_T4 - Fatal编程技术网

C# T4。错误:表达式块计算为空

C# T4。错误:表达式块计算为空,c#,.net,code-generation,t4,C#,.net,Code Generation,T4,我添加了template.tt文件,该文件如下所示: <#@ template language="C#" debug="true" #> <#@ output extension=".cs" #> <#@ import namespace="System" #> <#@ import namespace="System.Collections.Generic" #> using System; using System.Collections.

我添加了template.tt文件,该文件如下所示:

<#@ template language="C#" debug="true" #>
<#@ output extension=".cs" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Collections.Generic" #>

using System;
using System.Collections.Generic;

namespace Test
{
    public class <#= this.ClassName#>
    {       

    }
}

<#+
    public string ClassName { get; set; }
#>
我应该如何避免看到这些消息


提前感谢

问题是ClassName属性为空。修复错误的一种方法是将类功能块中的代码更改为:

<#+
    private string className = "";

    public string ClassName {
        get { return className; }
        set { className = value; }
    }
#>

我假设,您希望生成类似

using System;
using System.Collections.Generic;

namespace Test
{
    public class MyClass
    {       

    }
}
代码中的问题在表达式块中,您正在引用类要素块中不存在的变量
。修改代码如下

<#@ template language="C#" debug="true" #>
<#@ output extension=".cs" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Collections.Generic" #>

using System;
using System.Collections.Generic;

namespace Test
{
    public class <#= this.ClassName #> //Expression Block
    {       
    }
}

<#+ //Class feature block
    public string ClassName = "MyClass";
#>

使用制度;
使用System.Collections.Generic;
名称空间测试
{
公共类//表达式块
{       
}
}

事实上,它确实作为属性存在,但它的值为null,因为它尚未初始化。总的来说,一个属性在T4模板中的一个成员变量上并没有增加太多的值。我将完全跳过该属性,只需调用成员变量“ClassName”。我认为在模板中创建不可重用代码时,有必要重新评估一些日常编码规则的价值。尽管您正在用C#编写代码,但请将其更多地视为一个脚本环境。
<#@ template language="C#" debug="true" #>
<#@ output extension=".cs" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Collections.Generic" #>

using System;
using System.Collections.Generic;

namespace Test
{
    public class <#= this.ClassName #> //Expression Block
    {       
    }
}

<#+ //Class feature block
    public string ClassName = "MyClass";
#>