none
ApplicationSettingsBase in C# and .NET 2.0

    Question

  • Trying to use the Application settings to store data within a little application of mine but have encountered a few problems.

    The first, and is not that big of a deal, is that it seems that the property designer will not show me the types defined within my own application but only system types. Is this by design? I work around it by choosing "browse" any typing the type by hand.

    The bigger problem has to do with trying to save a list of classes as a single property.

    The first problem, I want to use a generic list (List<DealSourceInfo>) but the designer won't let me. Seems to have something to do with the fact that the < and > characters don't play well with the XML. I work around this by creating a little wrapper class.



    [Serializable]
    public class DealSourceInfoList : List<DealSourceInfo>
    {
    }

     


    Ok, now fixing this, the list does create an entry and attemps to save itself but no matter what I put in it, I always get an empty list when I reload the application. After digging for a while I discovered that the serialization of my structure throws an exception (which is hidden until you turn on a little magic flag within the ApplicationSettingsBase). The code for the class is like this.



    [Serializable]
    public class DealSourceInfo
    {
      public bool _Enabled;
      public bool Enabled
      {
        get { return _Enabled; }
        set { _Enabled = value; }
      }

      [NonSerialized]
      private IDealSource _DealSource;
      public IDealSource Source2
      {
        get { return _DealSource; }
        set { _DealSource = value; }
      }

      public DealSourceInfo()
      {
        _DealSource =
    null;
        _Name =
    "";
        _Enabled =
    true;
        _Active =
    true;
      }
    }

     



    The exception says that Source2 cannot be serialized. It doesn't exactly say why but I figure it is because it is an interface, which would make sense. But the _DealSource property is set to [NonSerializable] and I thought that serialization only worked on fields.

    I know I could probably work around this by seperating my other parameters by splitting them into a base class. But then again, I figure my code would work. Am I missing anything?

    BTW, I am using the RTM bits...

    Thanks!

    Thursday, October 27, 2005 2:52 AM