2011-04-13 66 views
1

在生成控制器,合同及其履行情況的WCF服務,我用的是錯誤的字符代碼生成和T4文本模板

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是一個泛型集合,會生成奇怪的字符並且生成的代碼將被破壞。

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

,似乎順利,裝配建立

如我第一次像生成控制器到BLL大會。 但是當我在這個程序集上使用內省來生成 WCF程序集中的代碼時,例如,產生像servicecontracts:

[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,比之前在底行符號越低。所有生成的包含通用返回類型的代碼都會發生這種情況。

introspector在這裏挑錯了什麼,或者它是一個編碼問題?

+2

這不是編碼問題,'PagedList'1 ' - 它是什麼泛型類型看起來像 ''1' - 意味着這是具有一種類型參數的泛型類型。你需要手動構建這個返回類型以獲得它的工作 – Stecya 2011-04-13 08:13:18

+0

啊哈,感謝這個非常有用的信息?請介意把它作爲答案嗎? – 2011-04-13 08:26:31

回答

3

這不是編碼問題,PagedList'1<Portal.BLL.BO.Employee> - 它是什麼泛型看起來像'1 - 意味着這是一種類型參數的泛型。你需要手動構建這個返回類型以使其工作