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

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

问题描述

我与许多拥有自定义类的成员有一个数据联系人

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?

推荐答案

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

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

来自 MSDN:当应用于方法时,指定在对象图中的对象反序列化期间调用该方法.相对于图中其他对象的反序列化顺序是不确定的.

这是我的示例代码:

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 中使用自定义序列化或反序列化在数据联系人的每个属性上强制一个新实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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