SvcUtil 生成自定义中间列表类型 - 有什么方法可以生成通用列表?
我正在使用 svcutil 从 XSD 生成数据契约类。以下是 XSD 中的片段:
<xs:element name="Fulfilment">
....
....
<xs:element name="Products" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded"
type="Product" name="Product" />
</xs:sequence>
</xs:complexType>
</xs:element>
生成的代码如下
public ProductsType Products
{
get
{
return this.ProductsField;
}
set
{
this.ProductsField = value;
}
}
public class ProductsType : System.Collections.Generic.List<Product>
{
}
:有什么方法可以让 svcutil 直接生成 Products
属性作为产品的通用列表,而不是创建从 list 继承的“ProductsType”类并使用它?
I'm using svcutil to generate datacontract classes from an XSD. Here's a snippet from the XSD:
<xs:element name="Fulfilment">
....
....
<xs:element name="Products" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded"
type="Product" name="Product" />
</xs:sequence>
</xs:complexType>
</xs:element>
Instead of the <Products>
elements being generated as a list property of the Fulfilment
object, the code that gets generated is this:
public ProductsType Products
{
get
{
return this.ProductsField;
}
set
{
this.ProductsField = value;
}
}
public class ProductsType : System.Collections.Generic.List<Product>
{
}
Is there any way to make svcutil generate the Products
property as a generic list of products directly, rather than creating the "ProductsType" class that inherits from list, and using that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
svcutil.exe http://localhost/Services/Sample.svc?wsdl /ct :System.Collections.Generic.List`1 如果这是您想要的答案,请勾选
There you go svcutil.exe http://localhost/Services/Sample.svc?wsdl /ct:System.Collections.Generic.List`1 If this is the answer you want, please tick
是的,当您在 VS 上添加服务引用时,您可以决定如何从 WCF 转换集合。
Yes, when you add a service reference on VS you can decide how to convert a collection from WCF.
请参阅 http://msdn.microsoft.com/en-us/library/aa347850 .aspx 有关集合序列化的详细讨论。
我问了一个类似的问题(svcutil 生成的代码中嵌套通用集合类是否有原因?)以及该 MSDN 文档为我回答了。只要您想使用 svcutil,似乎您就会陷入冗余的内部类中。我的结论是,由于它生成的代码和消费者的接口在这两种情况下都是相同的,所以我不会关心那个额外的类。
See http://msdn.microsoft.com/en-us/library/aa347850.aspx for a detailed discussion of serialization of collections.
I asked a similar question (Is there a reason for the nested generic collection classes in svcutil generated code?) and that MSDN document answered it for me. As long as you want to use svcutil, it appears you are stuck with the redundant internal class. My conclusion was that since it's generated code and the interface for the consumer is identical in both cases, I'm just going to not care about that extra class.