コンテンツへスキップ
ものがたり
戻る

huh? in IgnoreDataMemberAttribute

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute.aspx

The data contract model is an “opt-in” model. Applying the DataMemberAttribute to a field or property explicitly specifies that the member value will be serialized. In contrast, the BinaryFormatter serializes public and private fields of a type, and the XmlSerializer serializes only public fields and properties of a type. (下線筆者)

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ignoredatamemberattribute.aspx

Apply the IgnoreDataMemberAttribute attribute to opt-out of the default DataContractSerializer behavior. By default, the DataContractSerializer serializes all publicly visible types. All public read/write properties and fields of the type are serialized. You can change the default behavior by applying the DataContractAttribute and DataMemberAttribute attributes to the types and members. (下線筆者)

( ゚д゚)ポカーン

…いや、どう見ても前者が正しいです。本当にありがとうございました。

using System;  

using System.IO;  

using System.Runtime.Serialization;  

using System.Xml;

[DataContract]
public class Wrapper
{
[DataMember (Order = 1)]
public Test T;

[DataMember (Order = 2)]
public Test T2;
}

[DataContract]
public class Test
{
[DataMember]
public string F = “x”;

public static void Main ()
{
string ns = “http://schemas.microsoft.com/2003/10/Serialization/“;
var dc = new DataContractSerializer (typeof (Wrapper));
Test t = new Test ();
using (var xw = XmlWriter.Create (Console.Out)) {
xw.WriteStartElement (“z”, “root”, ns);
dc.WriteObject (xw, new Wrapper () {T = t, T2 = t});
xw.WriteEndElement ();
}
}
}

xx

コメント

arton — 11/24/2008 00:03:08

日本語のほうはコミュニティコンテンツで注をいれときました。http://msdn.microsoft.com/ja-jp/library/system.runtime.serialization.ignoredatamemberattribute.aspx
っていうか、こういうことですよね?

atsushieno — 11/24/2008 10:58:29

あ、ハイ、そういう話ですね。あとは、今の説明だとIgnoreDataMemberの存在意義が分からないので、その辺きちんと説明しる、っていう話になるかと思います。
まあ、この辺は自分で実装していたので、何となくどう使われているのか、想像はつきますが…(実験する気なしw)

arton — 11/24/2008 12:28:24

存在意義
そこは僕も疑問だったのですが(で、調べる気がない)、継承にからむのか、既定で全部DataMember化する属性があるのかなぁと思いました。

atsushieno — 11/24/2008 18:10:02

まあ多分この辺の事情が関係するんだろうと思います
http://d.hatena.ne.jp/atsushieno/20051008
http://d.hatena.ne.jp/atsushieno/20051018


この記事を共有:

前の記事
万引き後に車でひき逃げし死亡事故を起こして強盗殺人に問われた事例について
次の記事
誰かMono on BSDのメンテナンスやりませんか?