开发者

Wrong characters in Code Generation and T4 Text Templates

开发者 https://www.devze.com 2023-02-24 01:25 出处:网络
While generating controller开发者_如何转开发s, contracts and implementation for a WCF service, I use the

While generating controller开发者_如何转开发s, contracts and implementation for a WCF service, I use the

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

to get information about the underlying business object classes.

a relevant piece of code generate such a controller like:

excerpt from webservice.tt:

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

and normally generates something like

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

however

when introducing generics, where the meth.ReturnType.Name is a generic collection weird characters are generated and the generated code becomes broken.

eg I generate a controller into the BLL Assembly first like:

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

that results in:

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

that seems to go well and the assembly builds. But then when I use the introspection on this assembly to generate code in the WCF assembly, e.g. to generate servicecontracts like:

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

it generates wrong code:

[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);

mind the `1 (apostrophe and 1) after the returntypename, before the lower than symbol on the bottom line. This happens to all generated code that contain generic returntypes.

Does the introspector picks something up wrong here or is it an encoding problem?


It's not encoding problem, PagedList'1<Portal.BLL.BO.Employee> - it is what generic type looks like '1 - means that this is generic type with one type parametr. You need to manualy construct this return type in order to get it work

0

精彩评论

暂无评论...
验证码 换一张
取 消