通过WCF服务序列化通用类型 [英] Serialize Generic type over WCF Service

查看:70
本文介绍了通过WCF服务序列化通用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#类,定义如下:

I have a C# class defined as follows:

public class GenericItem<T>
{
  public List<T> Items { get; set; }

  public DateTime TimeStamp { get; set; }
} 

我正在服务器上创建此类的实例。然后,我试图通过WCF服务将其通过电线传递,如下所示:

I am creating an instance of this class on my server. I am then trying to pass it over the wire via a WCF service as shown here:

[OperationContract]
public GenericItem<MyCustomType> GetResult()
{
  GenericItem<MyCustomType> result = BuildGenericItem();
  return result;
}

此时一切编译都很好。当我在Silverlight应用程序中更新服务引用进行重新编译时,收到类似于以下内容的编译时错误:

Everything compiles just fine at this point. When I "update service reference" in my Silverlight app an re-compile, I receive a compile-time error, similar to the following:

MyNamespace.GenericItemOfMyCustomType [额外字符]不包含 GetEnumerator的公共定义

我不知道为什么:


  1. 出现了多余的字符。每次我更新服务参考时,似乎都在改变。

  2. 如何实际解决此问题。

我在做什么错了?

推荐答案

Sleiman是正确的,但是可以按照本文,您也许可以实现自己的目标想。这使您可以在服务中创建一个通用类型并公开它。但是,消费者不会将其视为通用的,因为服务操作中已指定类型。

Sleiman is correct, but one can use Bounded Generics as described in this article, and you may be able to achieve what you want. This allows you to create a generic type within the service and expose it. But the consumer will not view it as generic as the type is specified in the service operation.

这篇关于通过WCF服务序列化通用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆