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
654 views
in Technique[技术] by (71.8m points)

c# - How to use Custom Serialization or Deserialization in WCF to force a new instance on every property of a datacontact ?

I have a datacontact with many members that has a custom class

I would like to force a new instance if the property is null on deserialization.

is there a way to do that?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

If your are using DataContract serialization then you can override its default behaviour using the OnDeserialized attribute.

From MSDN: When applied to a method, specifies that the method is called during deserialization of an object in an object graph. The order of deserialization relative to other objects in the graph is non-deterministic.

Here is my sample code:

namespace MySpace
{

  public class MyCustomClass
  {
    public string MyStrData { get; set; }
  }

  [DataContract]
  public class Data
  {
    [DataMember]
    public int mInt;

    [DataMember]
    public MyCustomClass MyCustonObj;



    [OnDeserialized]
    void OnDeserialized(StreamingContext c)
    {
      if (MyCustonObj == null)
      {
        MyCustonObj = new MyCustomClass();
        MyCustonObj.MyStrData = "Overridden in serialization";
      }
    }

    [OnDeserializing]
    void OnDeserializing(StreamingContext c)
    {
      if (MyCustonObj == null)
      {
        MyCustonObj = new MyCustomClass();
        MyCustonObj.MyStrData = "Overridden in  deserializing";
      }
    }

    [OnSerialized]
    void OnSerialized(StreamingContext c)
    {
       // if you wan to  do somehing when serialized here or just remove them

    }

    [OnSerializing]
    void OnSerializing(StreamingContext c)
    {
       // if you wan to  do somehing during serializing here or just remove them    
    }
  }


}
[ServiceContract]
public interface IService
{
  [OperationContract]
  Data Method(Data dd);
}

public class Service : IService
{
  public Data Method(Data dd)
  {
    return dd;
  }
}

class Program
{
  static void Main(string[] args)
  {
    string Url = "http://localhost:8000/";
    Binding binding = new BasicHttpBinding();
    ServiceHost host = new ServiceHost(typeof(Service));
    host.AddServiceEndpoint(typeof(IService), binding, Url);
    host.Open();
    ChannelFactory<IService> fac = new ChannelFactory<IService>(binding);
    fac.Open();
    IService proxy = fac.CreateChannel(new EndpointAddress(Url));
    Data d = new Data();
    d.mInt = 5;
    Console.WriteLine("Data before calling service " + d.mInt);
    Console.WriteLine("Data before calling service " + (d.MyCustonObj == null ? "null" : d.MyCustonObj.MyStrData));
    d = proxy.Method(d);
    fac.Close();
    host.Close();
    Console.WriteLine("Data after calling service " + d.mInt);
    Console.WriteLine("Data after calling service " + d.MyCustonObj.MyStrData);
    Console.ReadLine();
  }
}

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

...