Esempio n. 1
0
        private static XmlNode CreateStatementNode(Statement e, XmlDocument xmlDoc)
        {
            XmlNode exNode = xmlDoc.CreateElement("Statement");

            exNode.Attributes.Append(CreateAttribute(xmlDoc, "type", GetTypeName(e.GetType())));
            PropertyInfo[] pinfo = e.GetType().GetProperties();
            foreach (PropertyInfo p in pinfo)
            {
                if (p.CanRead && p.CanWrite)
                {
                    object v = p.GetValue(e);
                    if (v is Expression)
                    {
                        XmlNode lnode = CreateNode(xmlDoc, p.Name, "type", v.GetType() + "");
                        lnode.AppendChild(CreateExpressionNode(v as Expression, xmlDoc));
                        exNode.AppendChild(lnode);
                    }
                    else if (v is Statement)
                    {
                        XmlNode lnode = CreateNode(xmlDoc, p.Name, "type", v.GetType() + "");
                        lnode.AppendChild(CreateStatementNode(v as Statement, xmlDoc));
                        exNode.AppendChild(lnode);
                    }
                    else if (v is List <Expression> )
                    {
                        XmlNode lnode = CreateNode(xmlDoc, p.Name, "type", v.GetType() + "");
                        foreach (Expression exp in (v as List <Expression>))
                        {
                            lnode.AppendChild(CreateExpressionNode(exp, xmlDoc));
                        }
                        exNode.AppendChild(lnode);
                    }
                    else if (v is List <string> )
                    {
                        XmlNode lnode = CreateNode(xmlDoc, p.Name, "type", v.GetType() + "");
                        foreach (string str in (v as List <string>))
                        {
                            lnode.AppendChild(CreateNode(xmlDoc, "String", "value", str + ""));
                        }
                        exNode.AppendChild(lnode);
                    }
                    else if (v is BlockStatement)
                    {
                        XmlNode lnode = CreateNode(xmlDoc, p.Name, "type", v.GetType() + "");
                        lnode.AppendChild(CreateBlockStatementNode(v as BlockStatement, xmlDoc));
                        exNode.AppendChild(lnode);
                    }
                    else if (v != null)
                    {
                        exNode.AppendChild(CreateNode(xmlDoc, p.Name, "value", v + ""));
                    }
                }
            }

            return(exNode);
        }
Esempio n. 2
0
        public static Statement Clone(this Statement st)
        {
            Type      t = st.GetType();
            Statement d = Activator.CreateInstance(t) as Statement;

            PropertyInfo[] pinf = t.GetProperties();
            foreach (PropertyInfo p in pinf)
            {
                if (p.CanRead && p.CanWrite)
                {
                    object pvalue = p.GetValue(st, null);
                    if (pvalue is Expression)
                    {
                        pvalue = Clone(pvalue as Expression);
                    }
                    else if (pvalue is BlockStatement)
                    {
                        pvalue = Clone(pvalue as BlockStatement);
                    }
                    else if (pvalue is Function)
                    {
                        pvalue = Clone(pvalue as Function);
                    }
                    if (pvalue == null)
                    {
                        continue;
                    }
                    if (pvalue is List <Expression> )
                    {
                        try
                        {
                            List <Expression> target = pvalue as List <Expression>;
                            List <Expression> nexps  = new List <Expression>();
                            foreach (Expression e in target)
                            {
                                nexps.Add(Clone(e));
                            }
                            p.SetValue(d, nexps, null);
                        }
                        catch (Exception ex) { }
                    }
                    else if (pvalue is List <string> )
                    {
                        List <string> target = pvalue as List <string>;
                        List <string> newStr = new List <string>();
                        foreach (string pv in target)
                        {
                            newStr.Add(pv);
                        }
                        p.SetValue(d, newStr, null);
                    }
                    else
                    {
                        p.SetValue(d, pvalue, null);
                    }
                }
            }
            return(d);
        }