代码生成和 T4 文本模板中的错误字符
在为 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);
}
however
的内容,其中 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);
请注意 returntypename 之后、底行低于符号之前的 `1 (撇号和 1)。所有包含通用返回类型的生成代码都会发生这种情况。
内省器是否在这里发现了错误,或者是编码问题?
While generating controllers, 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是编码问题,
PagedList'1
- 这就是泛型类型的样子'1
- 意味着这是具有一个的泛型类型类型参数您需要手动构造此返回类型才能使其正常工作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