public static Statement CloneStatement(Statement st) { return(st.Clone()); }
private static Statement LoadStatement(XmlNode root) { if (root.Name != "Statement") { throw new FormatException("Wrong data format"); } if (root.Attributes["type"] == null) { throw new FormatException("Wrong data format"); } string type = root.Attributes["type"].Value; Type t = Type.GetType(type); PropertyInfo[] inf = t.GetProperties(); Dictionary <string, PropertyInfo> pMap = new Dictionary <string, PropertyInfo>(); foreach (PropertyInfo p in inf) { pMap.Add(p.Name, p); } Statement st = Activator.CreateInstance(t) as Statement; foreach (XmlNode node in root.ChildNodes) { string pname = node.Name; PropertyInfo property = pMap[pname]; Type pType = property.PropertyType; if (pType.IsAssignableFrom(typeof(BlockStatement))) { property.SetValue(st, LoadBlockStatement(node.ChildNodes[0])); } else if (pType.IsAssignableFrom(typeof(Expression))) { property.SetValue(st, LoadExpression(node.ChildNodes[0])); } else if (pType.IsAssignableFrom(typeof(Statement))) { property.SetValue(st, LoadStatement(node.ChildNodes[0])); } else if (pType.IsAssignableFrom(typeof(List <Expression>))) { List <Expression> expressions = new List <Expression>(); foreach (XmlNode enode in node.ChildNodes) { expressions.Add(LoadExpression(enode)); } property.SetValue(st, expressions); } else if (pType.IsAssignableFrom(typeof(List <string>))) { List <String> str = new List <string>(); foreach (XmlNode snode in node.ChildNodes) { if (snode.Name == "String") { str.Add(snode.Attributes["value"].Value); } } property.SetValue(st, str); } else { string value = node.Attributes["value"].Value; object obj = ConvertTo(value, pType); if (obj != null) { property.SetValue(st, obj); } } } return(st); }