예제 #1
0
        /// <summary>
        /// 根据格式化的json串创建动态对象
        /// </summary>
        /// <param name="fromatjsonstring">格式化的json串,如下:
        /// {
        ///     key:{0},
        ///     key2:{1}
        /// }
        /// </param>
        ///  <param name="flag"></param>
        /// <param name="values">对象数组,下标对应json串中的{index}</param>
        /// <returns></returns>
        public static dynamic CreateInstanceFromat(string fromatjsonstring, FrameDLRFlags flag, params object[] values)
        {
            var context   = new List <KeyValuePair <string, object> >();
            var json      = fromatjsonstring;
            var validlist = new Dictionary <string, object>();
            var i         = 0;

            foreach (var item in values)
            {
                var pkey = "{" + i + "}";
                validlist.Add(pkey, item);
                var vkey = "fdo_p_" + i;
                json = json.Replace(pkey, vkey);
                context.Add(new KeyValuePair <string, object>("fdo_p_" + i, values[i]));
                i++;
            }

            FrameDLRObject rtn = CreateInstance(json, flag, context.ToArray());

            rtn.ori_jsongstring = fromatjsonstring;
            rtn._valuelist      = validlist;



            return(rtn);
        }
예제 #2
0
 /// <summary>
 /// 尝试将string转化成json对象
 /// </summary>
 /// <param name="json"></param>
 /// <param name="outobj"></param>
 public static void TryParse(string json, FrameDLRFlags flag, out FrameDLRObject outobj)
 {
     try
     {
         outobj = FrameDLRObject.CreateInstance(json, flag);
     }
     catch (Exception)
     {
         outobj = null;
     }
 }
예제 #3
0
 /// <summary>
 /// 判定是否为json对象
 /// </summary>
 /// <param name="json"></param>
 /// <returns></returns>
 public static bool IsJson(string json)
 {
     try
     {
         var v = FrameDLRObject.CreateInstance(json);
         return(true);
     }
     catch (Exception ex)
     {
         return(false);
     }
 }
예제 #4
0
        /// <summary>
        /// 创建一个DLR的新实例对象
        /// </summary>
        /// <param name="d"></param>
        /// <param name="flags"></param>
        /// <returns></returns>
        public static dynamic CreateInstance(Dictionary <string, object> d, FrameDLRFlags flags)
        {
            FrameDLRObject rtn = null;

            if (d != null)
            {
                rtn = BuildLoopDics(d, flags);
            }
            else
            {
                rtn    = new FrameDLRObject();
                rtn._d = new Dictionary <string, object>();
            }

            return(rtn);
        }
예제 #5
0
        /// <summary>
        /// 递归建立动态json对象
        /// </summary>
        /// <param name="d"></param>
        /// <param name="flags"></param>
        /// <returns></returns>
        private static dynamic BuildLoopDics(Dictionary <string, object> d, FrameDLRFlags flags)
        {
            FrameDLRObject rtn = new FrameDLRObject();

            if ((flags & FrameDLRFlags.SensitiveCase) != 0)
            {
                rtn.ignorecase = false;
            }
            else
            {
                rtn.ignorecase = true;
            }

            foreach (var item in d)
            {
                if (item.Value is Dictionary <string, object> )
                {
                    FrameDLRObject rtnloop = BuildLoopDics((Dictionary <string, object>)item.Value, flags);
                    rtn.SetValue(item.Key, (object)rtnloop);
                }
                else if (item.Value is object[])
                {
                    List <object> list = new List <object>();
                    foreach (var m in (object[])item.Value)
                    {
                        if (m is Dictionary <string, object> )
                        {
                            list.Add(BuildLoopDics((Dictionary <string, object>)m, flags));
                        }
                        else
                        {
                            list.Add(m);
                        }
                    }
                    rtn.SetValue(item.Key, list.ToArray());
                }
                else
                {
                    rtn.SetValue(item.Key, item.Value);
                }
            }

            return(rtn);
        }
예제 #6
0
        /// <summary>
        /// 创建动态对象,通过反射将model的property转成动态对象。
        /// </summary>
        /// <param name="model"></param>
        /// <param name="flags"></param>
        /// <returns></returns>
        public static dynamic CreateInstance(object model, FrameDLRFlags flags)
        {
            FrameDLRObject rtn = CreateInstance();

            if (model != null)
            {
                if (model is Dictionary <string, object> )
                {
                    rtn = BuildLoopDics((Dictionary <string, object>)model, flags);
                }
                else if (model is Dictionary <string, FrameDLRObject> )
                {
                    rtn = BuildLoopDics((Dictionary <string, FrameDLRObject>)model, flags);
                }
                else if (model is string)
                {
                    XmlDocument xd = null;
                    if (TryParseXml(ComFunc.nvl(model), out xd))
                    {
                        rtn = BuildLoopXml(xd.FirstChild.ChildNodes, flags);
                    }
                    else
                    {
                        rtn = CreateInstance(ComFunc.nvl(model), flags);
                    }
                    return(rtn);
                }
                else if (model is XmlDocument)
                {
                    rtn = BuildLoopXml(((XmlDocument)model).FirstChild.ChildNodes, flags);
                }
                else
                {
                    var t = model.GetType();
                    rtn.ignorecase = flags == FrameDLRFlags.SensitiveCase ? false : true;
                    foreach (var p in t.GetProperties(BindingFlags.Instance | BindingFlags.Public))
                    {
                        rtn.SetValue(p.Name, p.GetValue(model));
                    }
                }
            }
            return(rtn);
        }
예제 #7
0
        private static dynamic BuildLoopDics(Dictionary <string, FrameDLRObject> d, FrameDLRFlags flags)
        {
            FrameDLRObject rtn = new FrameDLRObject();

            if ((flags & FrameDLRFlags.SensitiveCase) != 0)
            {
                rtn.ignorecase = false;
            }
            else
            {
                rtn.ignorecase = true;
            }

            foreach (var item in d)
            {
                rtn.SetValue(item.Key, item.Value);
            }

            return(rtn);
        }
예제 #8
0
        /// <summary>
        /// 创建一个DLR的新实例对象
        /// </summary>
        /// <param name="flags"></param>
        /// <returns></returns>
        public static dynamic CreateInstance(FrameDLRFlags flags)
        {
            FrameDLRObject rtn = new FrameDLRObject();

            if (rtn._d == null)
            {
                rtn._d = new Dictionary <string, object>();
            }

            if ((flags & FrameDLRFlags.SensitiveCase) != 0)
            {
                rtn.ignorecase = false;
            }
            else
            {
                rtn.ignorecase = true;
            }

            return(rtn);
        }
예제 #9
0
        public object Clone()
        {
            FrameDLRObject rtn = FrameDLRObject.CreateInstance();

            rtn.ignorecase = this.ignorecase;

            foreach (var k in this.Keys)
            {
                var obj = this.GetValue(k);
                if (obj is ICloneable)
                {
                    rtn.SetValue(k, ((ICloneable)obj).Clone());
                }
                else
                {
                    rtn.SetValue(k, obj);
                }
            }

            return(rtn);
        }
예제 #10
0
        /// <summary>
        /// 通过xml建立动态对象,最简单的xml结构,如下:
        /// <xxx>
        ///     <n1></n1>
        ///     <n2></n2>
        ///     <nodes>
        ///         <nn1></nn1>
        ///         <nnn></nnn>
        ///     </nodes>
        ///     <nn></nn>
        /// </xxx>
        /// </summary>
        /// <param name="node"></param>
        /// <param name="flags"></param>
        /// <returns></returns>
        private static dynamic BuildLoopXml(XmlNodeList nodelist, FrameDLRFlags flags)
        {
            FrameDLRObject rtn = new FrameDLRObject();

            if ((flags & FrameDLRFlags.SensitiveCase) != 0)
            {
                rtn.ignorecase = false;
            }
            else
            {
                rtn.ignorecase = true;
            }

            foreach (XmlNode item in nodelist)
            {
                if (item.HasChildNodes && item.FirstChild.NodeType == XmlNodeType.Element)
                {
                    FrameDLRObject rtnloop = BuildLoopXml(item.ChildNodes, flags);
                    rtn.SetValue(item.Name, rtnloop);
                }
                else
                {
                    if (DateTimeStd.IsDateTime(item.InnerText))
                    {
                        rtn.SetValue(item.Name, DateTimeStd.ParseStd(item.InnerText).Value);
                    }
                    else
                    {
                        rtn.SetValue(item.Name, item.InnerText);
                    }
                }
            }


            return(rtn);
        }
예제 #11
0
 private string ToJSON(FrameDLRObject obj, int level)
 {
     return(ToJSON(obj._d, level));
 }
예제 #12
0
        private Dictionary <string, object> ToDics(FrameDLRObject obj)
        {
            Dictionary <string, object> rtn = new Dictionary <string, object>();

            foreach (var s in obj._d)
            {
                if (s.Value is FrameDLRObject)
                {
                    rtn.Add(s.Key, ToDics((FrameDLRObject)s.Value));
                }
                else if (s.Value is List <FrameDLRObject> )
                {
                    List <object> list = new List <object>();
                    foreach (var m in (List <FrameDLRObject>)s.Value)
                    {
                        if (m is FrameDLRObject)
                        {
                            list.Add(ToDics((FrameDLRObject)m));
                        }
                        else
                        {
                            list.Add(m);
                        }
                    }
                    rtn.Add(s.Key, list.ToArray());
                }
                else if (s.Value is List <object> )
                {
                    List <object> list = new List <object>();
                    foreach (var m in (List <object>)s.Value)
                    {
                        if (m is FrameDLRObject)
                        {
                            list.Add(ToDics((FrameDLRObject)m));
                        }
                        else
                        {
                            list.Add(m);
                        }
                    }
                    rtn.Add(s.Key, list.ToArray());
                }
                else if (s.Value is object[])
                {
                    List <object> list = new List <object>();
                    foreach (var m in (object[])s.Value)
                    {
                        if (m is FrameDLRObject)
                        {
                            list.Add(ToDics((FrameDLRObject)m));
                        }
                        else
                        {
                            list.Add(m);
                        }
                    }
                    rtn.Add(s.Key, list.ToArray());
                }
                else
                {
                    rtn.Add(s.Key, s.Value);
                }
            }
            return(rtn);
        }
예제 #13
0
 /// <summary>
 /// 尝试将string转化成json对象
 /// </summary>
 /// <param name="json"></param>
 /// <param name="outobj"></param>
 public static void TryParse(string json, out FrameDLRObject outobj)
 {
     TryParse(json, FrameDLRFlags.None, out outobj);
 }
예제 #14
0
        /// <summary>
        /// 根据json串创建动态对象
        /// </summary>
        /// <param name="jsonstring"></param>
        /// <param name="flags"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static dynamic CreateInstance(string jsonstring, FrameDLRFlags flags, params KeyValuePair <string, object>[] context)
        {
            FrameDLRObject rtn = null;

            if (!string.IsNullOrEmpty(jsonstring))
            {
                HostJs jse = HostJs.NewInstance();
                try
                {
                    XmlDocument xd = null;
                    if (jsonstring.Trim().StartsWith("<") && TryParseXml(ComFunc.nvl(jsonstring), out xd))
                    {
                        if (xd.FirstChild.NodeType == XmlNodeType.XmlDeclaration)
                        {
                            if (xd.ChildNodes.Count == 2)
                            {
                                rtn = BuildLoopXml(xd.ChildNodes[1].ChildNodes, flags);
                            }
                            else
                            {
                                rtn = BuildLoopXml(xd.ChildNodes, flags);
                            }
                        }
                        else
                        {
                            if (xd.ChildNodes.Count == 1)
                            {
                                rtn = BuildLoopXml(xd.FirstChild.ChildNodes, flags);
                            }
                            else
                            {
                                rtn = BuildLoopXml(xd.ChildNodes, flags);
                            }
                        }
                    }
                    else
                    {
                        string js = "var out=" + jsonstring;
                        if (context != null)
                        {
                            jse.Evaluate(js, context);
                        }
                        else
                        {
                            jse.Evaluate(js);
                        }
                        var d = (Dictionary <string, object>)jse.GetOutObject("out");
                        rtn = BuildLoopDics(d, flags);
                    }
                }
                finally
                {
                    jse.Release();
                }
            }
            else
            {
                rtn = CreateInstance();
            }
            rtn.ori_jsongstring = jsonstring;
            return(rtn);
        }