Ejemplo n.º 1
0
        public static T Deserialize <T>(NestedDictionary dict, params object[] ctorArgs)
        {
            T obj;

            if (!dict.ContainsKey("$type") && dict.Count > 1)
            {
                try
                {
                    obj = (T)Activator.CreateInstance(typeof(T), ctorArgs);
                }
                catch (Exception)
                {
                    obj = (T)Activator.CreateInstance(typeof(T));
                }
            }
            else if (dict.ContainsKey("$type"))
            {
                var type = Assembly.GetAssembly(typeof(T)).GetType(dict["$type"]);
                if (type.GetConstructor(ctorArgs.Select(a => a.GetType()).ToArray()) != null)
                {
                    obj = (T)Activator.CreateInstance(type, ctorArgs);
                }
                else
                {
                    obj = (T)Activator.CreateInstance(type);
                }
            }
            else
            {
                return(default(T));
            }

            DeserializeDynamic(dict, obj, ctorArgs);
            return(obj);
        }
Ejemplo n.º 2
0
        private static void AddNode(List <NestedDictionary> p_ndf, ref NestedDictionary p_current_dict, ref NestedDictionaryNode p_current_node)
        {
            string p_node_key = p_current_node.Key;

            if (p_node_key == null)
            {
                return;                     // only occurs should the "node" be a reference
            }
            if (!p_current_dict.ContainsKey(p_node_key))
            {
                p_current_dict.Add(p_node_key, p_current_node);
            }
            else
            {
                p_ndf.Add(p_current_dict);
                p_current_dict = new NestedDictionary();
                p_current_dict.Add(p_node_key, p_current_node);
            }
            p_current_node = new NestedDictionaryNode();
        }