2009-10-21 88 views
7

鑑於正確的「MagicName」(它類似於「CanSerialize」),下面的代碼將爲空列表壓制xml。禁止Xml序列化的空列表

那是什麼神奇的名字?你指的是ShouldSerialize*PropertyName*方法命名約定,但據我所知,這並非指到XML序列化,但在Windows窗體組件屬性系列化

public class MyClass { 
    public List<int> MyList{ get; set; } 
    public bool MyListMagicName() { return MyList.Count != 0; } 
    public MyClass() { MyList = new List<int>(); } 
} 

回答

7

我的事(我可能是錯的呢)。看到這裏:http://msdn.microsoft.com/en-us/library/53b8022e%28VS.71%29.aspx

UPDATE。它似乎也適用於XML序列化,但它是一個未公開的功能:http://horacegoescoding.blogspot.com/2009/04/using-shouldserialize-for-conditional.html

+0

Markdown評論:我希望「ShouldSerialize」部分爲粗體,「PropertyName」部分爲粗體和斜體,不包圍星號。有趣的是,在編輯模式下顯示的預覽窗口顯示了預期的格式! – Konamiman 2009-10-21 10:24:29

+0

另請參閱:http://www.devolutions.net/articles/serialization.aspx#S222 – 2009-10-21 10:24:34