Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Microsoft.SPOT.Reflection.Serialize() / Deserialize() throws System.NotImplementedException


  • Please log in to reply
25 replies to this topic

#21 Fabien Royer

Fabien Royer

    Advanced Member

  • Members
  • PipPipPip
  • 406 posts
  • LocationRedmond, WA

Posted 03 February 2011 - 02:11 AM

Ok. Looks straight forward. Thanks for the details. I'll give this a try this weekend and will follow-up on the thread. Cheers, -Fabien.

#22 Brandon G

Brandon G

    Advanced Member

  • Members
  • PipPipPip
  • 92 posts
  • LocationVancouver BC, Canada

Posted 03 February 2011 - 04:10 AM

like i said, aint great but one thing i have noticed about netmf is a major need for optimization and special cases, elegance is sacrificed for speed

#23 Fabien Royer

Fabien Royer

    Advanced Member

  • Members
  • PipPipPip
  • 406 posts
  • LocationRedmond, WA

Posted 13 February 2011 - 10:22 PM

Hi Brandon, I wanted to let you know that I haven't forgotten about you :) I've been slammed with work lately with little time for my growing backlog of personal projects... I'll circle back here as soon as I can. Cheers, -Fabien.

#24 Brandon G

Brandon G

    Advanced Member

  • Members
  • PipPipPip
  • 92 posts
  • LocationVancouver BC, Canada

Posted 14 February 2011 - 12:07 AM

not a problem Fabien, same goes here. I am really not happy with what i've posted so far and starting to think aboutserialization in terms of config and xml and using weak references space lately

#25 Solidus

Solidus

    Member

  • Members
  • PipPip
  • 12 posts

Posted 05 March 2011 - 12:18 PM

Tangentially related to the current discussion, but I have a new project which requires Serialization (currently uses System.Reflection), so looking forward to seeing how you guys go :)

#26 Randy Bullock

Randy Bullock

    New Member

  • Members
  • Pip
  • 2 posts

Posted 09 October 2011 - 09:29 PM

I am working on a project that also needs serialization here is the solution I came up with so far. Just create a class that inherits Serializable, and call .Serialize() or .Deserialize()

I am using a KeyValuePair approach to serializing over xml, simply because it fits my needs better. In addition I am using microlinq in the Reflect class, if you don’t want to use this library you will just have to remove the .where call with a loop and write some condition logic.

Next steps for me is to write a serialized object to a micro SD card, I will post an updated version once I finish. Hope this helps, I'm Interested to get your feedback.

using System;
using Microsoft.SPOT;

namespace System.Runtime.CompilerServices {
    class ExtensionAttribute : Attribute{
    }
}

using System;
using Microsoft.SPOT;

namespace IDMProd.Core.Commons.Extensions {
    public static class StringExtensions {
        public static bool StartsWith(this string s1, string s2) {
            return (s1.Substring(0, s2.Length) == s2 ? true : false);
        }
    }
}

using System;
using Microsoft.SPOT;
using System.Collections;

namespace IDMProd.Core.Commons.Serialization {
    public interface ISerializable {
        ArrayList Serialize();
        void Deserialize(ArrayList serializedArray);
    }
}

using System;
using Microsoft.SPOT;
using System.Collections;

namespace IDMProd.Core.Commons.Serialization {
    public abstract class Serializable : ISerializable {
        public ArrayList Serialize() {
            return Reflect.GetPropertyKeyValuePairs(this);
        }

        public void Deserialize(System.Collections.ArrayList serializedArray) {
            foreach (KeyValuePair item in serializedArray) {
                Reflect.SetPropertyValue(this, item.Key, item.Value);
            }
        }
    }
}

using System;
using Microsoft.SPOT;
using System.Reflection;
using System.Collections;
using VikingErik.NetMF.MicroLinq;
using IDMProd.Core.Commons.Extensions;


namespace IDMProd.Core.Commons.Serialization {
    public static class Reflect {
        public static bool SetPropertyValue(object obj, string propertyName, object value) {
            try {
                Type t = obj.GetType();
                MethodInfo m = t.GetMethod("set_" + propertyName, BindingFlags.Public | BindingFlags.Instance);
                m.Invoke(obj, new object[] { value });
            }
            catch (Exception ex) {
                //TODO:Add error handling/logging
                return false;
            }
            return true;
        }

        public static object GetPropertyValue(object obj, string propertyName) {
            try {
                Type t = obj.GetType();
                MethodInfo m = t.GetMethod("get_" + propertyName, BindingFlags.Public | BindingFlags.Instance);
                return m.Invoke(obj, null);
            }
            catch (Exception ex) {
                //TODO:Add error handling/logging
                return null;
                //throw;
            }
        }

        public static ArrayList GetPropertyKeyValuePairs(object obj) {
            ArrayList valuePairs = new ArrayList();
            Type t = obj.GetType();
            MethodInfo[] methods = t.GetMethods(BindingFlags.Public | BindingFlags.Instance);
            int cnt = methods.Where(m => ((MethodInfo)m).Name.StartsWith("get_")).Count();
            foreach (MethodInfo item in methods.Where(m => ((MethodInfo)m).Name.StartsWith("get_"))) {
                try {
                    KeyValuePair kvp = new KeyValuePair();
                    kvp.Key = item.Name.Substring(4);
                    kvp.Value = item.Invoke(obj, null);
                    valuePairs.Add(kvp);
                }
                catch (Exception ex) {
                    //TODO: Error handling/logging
                    //throw;
                }
            }
            return valuePairs;
        }
    }
}

using System;
using Microsoft.SPOT;

namespace IDMProd.Core.Commons {
    public class KeyValuePair {
        public string Key { get; set; }
        public object Value { get; set; }

        public KeyValuePair() {
        }

        public KeyValuePair(string key, object value) {
            this.Key = key;
            this.Value = value;
        }
    }
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.