代码生成和T4文本模板中的错误字符



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

 Microsoft FxCop 1.35FxCopSdk.dll
 Microsoft FxCop 1.35Microsoft.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组件,例如生成serviceconcontracts:

    [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)在returntypename之后,在最底层的符号之前。这将发生在所有包含泛型返回类型的生成代码中。

内省器在这里拾取错误的东西还是编码问题?

这不是编码问题,PagedList'1<Portal.BLL.BO.Employee> -它是泛型类型看起来像'1 -意味着这是一个类型参数的泛型类型。您需要手动构造此返回类型才能使其工作

最新更新