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.
Microsoft.SPOT.Reflection.Serialize() / Deserialize() throws System.NotImplementedException
Started by Fabien Royer, Jan 13 2011 01:27 AM
25 replies to this topic
#22
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
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
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
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
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.
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