<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ import namespace="System" #>
<#@ output extension=".cs" #>
namespace TestApp
{
<#
var type = Type.GetType("TestApp.MyClass", false);
var typeName = type == null ? "Null" : type.Name;
#>
public partial class <#= typeName #>
{
// ...
}
}
进入生成类Null
提示为什么看不到?
因为 T4 不在您的应用程序上下文中运行,而是在它自己的上下文中运行。因此,在 T4 的执行上下文中没有类型
TestApp.MyClass,Type.GetType也无法找到它。T4 不是您的应用程序,它是创建应用程序文本的代码。