Jūs esateŽurnalai / Ernestas Kardzys's blog / C#. Object -> Dictionary -> Object

C#. Object -> Dictionary -> Object


ParašėErnestas Kardzys - 2009 Birželio 13

I have the fallowing situation. I have an object. I need to make a Dictionary, which contains all the properties (and it's values) of the object. Then send this Dictionary over the network. In client side the Dictionary will be received and then all the data from Dictionary will be put to an object. It's also possible to serialize an object and then send it over the network (without making a Dictionary object) but we decided that it's better to make a Dictionary. So, the code:
class PackageMaker { #region Methods /// <summary> /// Scans object and adds entries to a dictionary /// </summary> /// <param name="serializableObject">Object needs to be scanned</param> /// <returns>Dictionary with properties and their values</returns> public Dictionary<string, string> AddToPackage(Object serializableObject) { if (serializableObject == null) throw new ArgumentNullException("serializableObject is not initialized!"); Dictionary<string, string> package = new Dictionary<string, string>(); foreach (PropertyInfo packetInfo in serializableObject.GetType().GetProperties()) { package.Add(packetInfo.Name, packetInfo.GetValue(serializableObject, null).ToString()); } return package; } /// <summary> /// Extracts data from dictionary and sets the data to a object /// </summary> /// <param name="packet">Dictionary with properties and their values</param> /// <param name="deserializedObject">Object which set values</param> public void MakeObjectFromPackage(Dictionary<string, string> packet, Object deserializedObject) { if (deserializedObject == null) throw new ArgumentNullException("serializableObject is not initialized!"); foreach (KeyValuePair<string, string> entry in packet) { if (deserializedObject.GetType().GetProperty(entry.Key) != null) { deserializedObject.GetType().GetProperty(entry.Key).SetValue(deserializedObject, entry.Value, null); } } } #endregion }
It looks like it's working :)

Interesting code.

Yeah, that's correct. :)
We were discussing this problem. The main point is, that there might be lots of classes, so we thought it would be better to send one big object then lots of small ones.

"It’s also possible to serialize an object and then send it over the network (without making a Dictionary object) but we decided that it’s better to make a Dictionary."

And why the fuck is that? You just basically reimplemented serialization...

Skelbti naują komentarą

Šio laukelio turinys bus laikomas privatus ir nerodomas viešai.
  • Web puslapiu adresai ir el. pašto adresai automatiškai tampa nuorodomis.
  • Leidžiamos HTML žymės: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Linijos ir paragrafai atskiriami automatiškai
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].

Daugiau informacijos apie teksto formatavimą

CAPTCHA
Šis klausimas yra skirtas įsitikinti, jog jūs esate žmogus, ir sustabdyti automatinį šlamšto siuntimą.
5 + 5 =
Išspręskite šią paprastą matematinę užduotį ir įveskite atsakymą. Pvz., jei užduotis yra 1+3, įveskite 4.