Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

c# - Serialize Generic type over WCF Service

I have a C# class defined as follows:

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

  public DateTime TimeStamp { get; set; }
} 

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

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[extra chars] does not contain a public definition for 'GetEnumerator'

I have no idea why:

  1. Extra chars are appearing. The seem to change everytime I update the service reference.
  2. How to actually fix this.

What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...