0

I have a .net 4 class which it's decorated with a ReadOnly attribute. I'm trying to serialize this class in .NET Compact 3.5 project, but I'm getting an error: "There was an error reflecting type IpSettings" As far as I know .NET CF doesn't include any custom attributes, but I don't need to serialize this attribute. Is there any way to skip the attribute serialization ? Thanks, Alex

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; }

}

var serializer = new System.Xml.Serialization.XmlSerializer(typeof(IpSettings));

Alex Troto
  • 651
  • 6
  • 18

2 Answers2

0

You can control xml serialization via attributes in the .NET CF. To have the serialization system ignore a property, you can decorate it with the XmlIgnore attribute:

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; }

}
pdriegen
  • 2,019
  • 1
  • 13
  • 19
  • I want to serialize IP property, but without [ReadOnly(true)] attribute. [System.Xml.Serialization.XmlIgnore] will completely ignore IP property from serialization. – Alex Troto Jul 26 '12 at 13:04
  • Sorry Alex. I completely mis-interpreted what you were asking. I'm not sure why you need what you're asking though, as xmlserialization doesn't serialize any object meta-data such as attributes. – pdriegen Jul 26 '12 at 13:13
  • the problem I belive it's not with serialization, but with reflection. When XmlSerializer try to reflect IpSettings class , it's failing, because in .net CF there is not attribute as [ReadOnly]. This it's my guess. – Alex Troto Jul 26 '12 at 13:15
  • I'm struggling to understand how you can even load that type into a .NET CF application, as you should get a compile error. If you're loading it all dynamically somehow, can you put a pre-processor directive on your class implementation around that attribute (i.e. #IF !POCKET_PC [ReadOnly(true)] #endif – pdriegen Jul 26 '12 at 13:21
  • Hmm...I this you just gave me an ideea with pre-processor directive ! – Alex Troto Jul 26 '12 at 13:30
0

I've found I often have to rethink how I approach a problem when trying to see how I will tackle something for the Compact Framework.

Consider something like the following code. It still allows your string IP value to be readonly:

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;
    }

}

This will give you, as the programmer, flexibility in your class while still maintaining the readonly attributes of your IP field.