开发者

Custom attribute to switch property serialization to NetDataContractSerializer

开发者 https://www.devze.com 2022-12-14 00:16 出处:网络
In .NET 3.5, I would like to create a custom attribute (say [NetDataMember]) that would switch the serialization behavior from DataContractSerializer to NetDataContractSerializer.

In .NET 3.5, I would like to create a custom attribute (say [NetDataMember]) that would switch the serialization behavior from DataContractSerializer to NetDataContractSerializer.

Basically, for a class A as illustrated below

[DataContract]
class A
{
  [DataMember]
  public int SimpleProperty { get; set; }

  [Transcient]
  public IBar ComplexProperty { get; set; }
}

I would like to obtain a serializer that would behave like DataContractSerializer by default, but that would be overriden with Net开发者_C百科DataContractSerializer for properties marked with [NetDataMember].

Any idea how to design a serializer that would achieve such behavior?


There's no "out-of-the-box" way in WCF to do this - but a lot of really smart people have already tackled that problem.

Check out Aaron Skonnard's blog post on the NetDataContractSerializer in which he present a behavior you can put on your data contracts as an attribute:

[NetDataContractFormat] 

on your service interface (for all methods) or on a single method will use the NetDataContractSerializer for that call. You need to define this per operation or service - not on your data contracts.

0

精彩评论

暂无评论...
验证码 换一张
取 消