如何使用自定义序列化和反序列化在WCF强制在datacontact的每个属性的新实例? [英] How to use Custom Serialization or Deserialization in WCF to force a new instance on every property of a datacontact ?

查看:626
本文介绍了如何使用自定义序列化和反序列化在WCF强制在datacontact的每个属性的新实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多成员的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.

是有办法做到这一点?

推荐答案

如果您正在使用DataContract序列化,那么你可以覆盖使用 OnDeserialized 属性的默认行为。

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

从<一个href=\"http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializingattribute.aspx\"相对=nofollow> MSDN :在当施加到一个方法,则指定在一个对象图的对象的反序列化过程的方法被调用。相对于图中的其他对象反序列化的顺序是不确定的。

下面是我的示例code:

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

这篇关于如何使用自定义序列化和反序列化在WCF强制在datacontact的每个属性的新实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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