MobileBindingList<String> causes InvalidOperationException

MobileBindingList<String> causes InvalidOperationException

Old forum URL: forums.lhotka.net/forums/t/9611.aspx


marthac posted on Thursday, October 07, 2010

I received this error when I try to use MobileBindingList<String>:

"Cannot serialize collections not of type IMobileObject"

It is because of this line in MobileBindingList.cs:

      if (!typeof(IMobileObject).IsAssignableFrom(typeof(T)))
        throw new InvalidOperationException(Resources.CannotSerializeCollectionsNotOfIMobileObject);

My question is, why can I use the String type in a MobileDictionary, but I cannot use it in a MobileBindingList?

marthac replied on Thursday, October 07, 2010

Nevermind. Solved my problem by deriving from MobileBindingList<String> and overriding the OnGetChildren() & OnSetChildren() functions to get it to work.

Here's my class in case anyone else needs to do a similar thing:

    [Serializable]
    public class MyMobileBindingStringList : MobileBindingList<String>
    {
        public MyMobileBindingStringList() { }
        protected override void OnGetChildren(SerializationInfo info, MobileFormatter formatter)
        {
            for (int index = 0; index < this.Count; index++)
            {
                String value = this[index];
                if (value != null)
                {
                    info.AddValue("value" + index, value);
                }
            }
            info.AddValue("count", this.Count);
        }

        protected override void OnSetChildren(SerializationInfo info, MobileFormatter formatter)
        {
            int count = info.GetValue<int>("count");

            for (int index = 0; index < count; index++)
            {
                String value = info.GetValue<String>("value" + index);
                Add(value);
            }
        }
    }

Copyright (c) Marimer LLC