简体   繁体   English

如何在WCF中使用自定义序列化或反序列化来强制datacontact的每个属性上的新实例?

[英]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 我有一个datacontact,其中有许多成员都有自定义类

I would like to force a new instance if the property is null on deserialization. 如果在反序列化时属性为null,我想强制一个新实例。

is there a way to do that? 有没有办法做到这一点?

If your are using DataContract serialization then you can override its default behaviour using the OnDeserialized attribute. 如果您正在使用DataContract序列化,则可以使用OnDeserialized属性覆盖其默认行为。

From MSDN : When applied to a method, specifies that the method is called during deserialization of an object in an object graph. MSDN应用于方法时,指定在对象图中对象的反序列化期间调用该方法。 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();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM