2012-07-26 135 views
0

我有一個.net 4類,它用ReadOnly屬性裝飾。我試圖在.NET Compact 3.5項目中序列化這個類,但我得到一個錯誤: 「有一個錯誤,反映類型IpSettings」 據我所知.NET CF不包括任何自定義屬性,但我不需要序列化這個屬性。有沒有辦法跳過屬性序列化? 謝謝, 亞歷 .net compact framework deserialization

 
public class IpSettings 
    { 
     [ReadOnly(true)] 
     public string IP { get; set; }

public string Mask { get; set; } public string Gateway { get; set; } public string DNS1 { get; set; } public string DNS2 { get; set; } }

變種串行=新System.Xml.Serialization.XmlSerializer(typeof運算(IpSettings));

回答

0

您可以通過.NET CF中的屬性控制xml序列化。有序列化系統忽略的屬性,你可以用XmlIgnore屬性裝飾它:

public class IpSettings 
{ 

    [System.Xml.Serialization.XmlIgnore] 
    public string IP { get; set; } 


    public string Mask { get; set; } 

    public string Gateway { get; set; } 

    public string DNS1 { get; set; } 

    public string DNS2 { get; set; } 

} 
+0

我想序列化IP屬性,但沒有[ReadOnly(true)]屬性。 [System.Xml.Serialization.XmlIgnore]將完全忽略序列化的IP屬性。 – 2012-07-26 13:04:50

+0

對不起亞歷克斯。我完全錯誤地解釋你在問什麼。我不知道爲什麼你需要你所要求的,因爲xmlserialization不會序列化任何對象元數據,如屬性。 – pdriegen 2012-07-26 13:13:39

+0

問題我相信它不是用序列化,而是用反射。 當XmlSerializer嘗試反映IpSettings類時,它失敗了,因爲在.net CF中沒有屬性爲[ReadOnly]。這是我的猜測。 – 2012-07-26 13:15:50

0

我發現我經常不得不重新考慮我如何處理的問題想看看我怎麼會解決的東西的時候Compact Framework。

考慮下面的代碼。它還可以讓你的字符串值IP只讀

public class IpSettings 
{ 

    private string ip; 

    public IpSettings() 
    { 
    } 

    public IpSettings(string ipAddress) 
    { 
     ip = ipAddress; 
    } 

    public string IP { get { return ip; } } 

    public string Mask { get; set; } 

    public string Gateway { get; set; } 

    public string DNS1 { get; set; } 

    public string DNS2 { get; set; } 

    public static IpSettings Load() { 
     var ipSetting = new IpSettings(); 
     // code to load your serialized settings 
     ipSettings.ip = // some value you just read 
     return ipSettings; 
    } 

} 

這會給你,作爲程序員,靈活類,同時仍保持您的IP只讀屬性。