【问题标题】:Wrong characters in Code Generation and T4 Text Templates代码生成和 T4 文本模板中的错误字符
【发布时间】:2011-04-13 07:44:06
【问题描述】:

在为 WCF 服务生成控制器、契约和实现时,我使用了

 Microsoft FxCop 1.35\FxCopSdk.dll
 Microsoft FxCop 1.35\Microsoft.Cci.dll

获取有关底层业务对象类的信息。

一段相关的代码生成这样一个控制器,如:

摘自 webservice.tt:

    public <#=meth.ReturnType.Name#> <#=meth.Name #> (<#=parametersIn#>) {
        return <#=meth.DeclaringType.Name#>.<#=meth.Name#>(<#=parametersOut#>);
    }

通常会生成类似

的东西
    public Employee GetEmployee (Int64 id) {
        return EmployeeController.GetEmployee(id);
    }

然而

在引入泛型时,meth.ReturnType.Name 是泛型集合,生成了奇怪的字符,生成的代码被破坏了。

例如,我首先在 BLL 程序集中生成一个控制器,例如:

    public static PagedList<<#=t.Name#>> 
    GetAll<#=t.Name#>s(string sortby, int pageindex, int pagesize) {
        return <#=t.Name#>.GetPaged(sortby, pageindex, pagesize);
    }

导致:

    public static PagedList<Employee> 
    GetAllEmployees(string sortby, int pageindex, int pagesize) {
        return Employee.GetPaged(sortby, pageindex, pagesize);
    }

这似乎进展顺利并且程序集构建。 但是当我使用这个程序集的内省来生成代码时 WCF 程序集,例如生成服务合同,例如:

    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "<#=meth.Name#><#=parametersTemplate#>")]
    <#=meth.ReturnType.Name#> <#=meth.Name#> (<#=parametersIn#>);

生成错误代码:

[OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json, 
    RequestFormat = WebMessageFormat.Json, 
    BodyStyle = WebMessageBodyStyle.Wrapped, 
    UriTemplate = "GetAllEmployees?sortby={sortby}&pageindex={pageindex}&pagesize={pagesize}")]
    PagedList`1<Portal.BLL.BO.Employee> GetAllEmployees (String sortby, Int32 pageindex, Int32 pagesize);

注意返回类型名称后面的 `1(撇号和 1),在底行的低于符号之前。这发生在所有生成的包含通用返回类型的代码上。

内省者在这里发现了什么问题还是编码问题?

【问题讨论】:

  • 不是编码问题,PagedList'1&lt;Portal.BLL.BO.Employee&gt; - 这是泛型类型的样子'1 - 表示这是具有一个类型参数的泛型类型。您需要手动构造此返回类型才能使其正常工作
  • 啊哈,感谢您提供这些非常有用的信息?介意在这里作为答案吗?

标签: c# templates generics t4


【解决方案1】:

这不是编码问题,PagedList'1&lt;Portal.BLL.BO.Employee&gt; - 这是泛型类型的样子 '1 - 意味着这是具有一个类型参数的泛型类型。您需要手动构造此返回类型才能使其正常工作

【讨论】:

    猜你喜欢
    • 2013-10-03
    • 2010-09-22
    • 1970-01-01
    • 2017-06-06
    • 2011-12-04
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    • 2013-04-26
    相关资源
    最近更新 更多