您好,我刚开始使用 T4 模板,我需要根据 Controller 中的操作生成一个 javascript 文件。
我把代码弄明白了,忘记了 Controller 和操作,我唯一的问题是我在 T4 模板文件中遇到了这个错误,我不明白它:
Compiling transformation: A namespace cannot directly contain members such as fields or methods
这是我的代码:
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="$(TargetPath)" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="eConnect.WebApi.Helpers.T4.ControllerDetails" #>
<#@ import namespace="System.Web.Http;"#>
<#@ output extension=".js" #>
define(['services/logger',
'services/jsonDataService',
'services/config',
'services/cachingService'],
function (logger, jsonDataService, config, cache) {
var dataService = { };
return dataService;
});
<#
var controllers = ControllersInfo.GetControllers();
foreach(var controller in controllers) {
Dictionary<string, ParameterInfo[]> actions = ControllersInfo.GetAllCustomActionsInController(controller, new HttpGetAttribute());
}
#>
还有一个外部类可以获取 Controller 和操作,但我认为对于当前的问题来说这不是必需的。
我做错了什么?
最佳答案
你现在可能已经明白了,但是:
<#@ import namespace="System.Web.Http;"#>
注意';'
改为写:
<#@ import namespace="System.Web.Http"#>
关于c# - T4 命名空间不能直接包含字段或方法等成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16955106/