/// <summary> /// attribute字符串 /// </summary> private static string GetAttributeStr(List <System.Reflection.PropertyInfo> list, object sender, bool sort = false) { StringBuilder sb = new StringBuilder(50); if (sort) { list = list.OrderBy(c => { int flag = 10000; object[] custattributes = c.GetCustomAttributes(typeof(RequsetNode), true); if (custattributes != null && custattributes.Count() > 0) { RequsetNode n = custattributes[0] as RequsetNode; if (n != null) { return(n.Sort); } } return(flag); }).ToList(); } foreach (var info in list) { object[] custattributes = info.GetCustomAttributes(typeof(RequsetNode), true); if (custattributes != null && custattributes.Count() > 0) { RequsetNode n = custattributes[0] as RequsetNode; if (n != null) { object val = info.GetValue(sender, null); if (!n.AllowEmpty)//不允许空 { if (val == null || (val != null && string.IsNullOrEmpty(val.ToString()))) { throw new Exception(string.Format("属性{0}值不能为空!", info.Name)); } } if (val != null && !string.IsNullOrEmpty(val.ToString())) { sb.AppendFormat("{0}=\"{1}\" ", n.AttrName, val.ToString()); } } } } return(sb.ToString()); }
/// <summary> /// 同一个node下有attribute的属性 /// </summary> private static List <System.Reflection.PropertyInfo> GetAttributes(List <System.Reflection.PropertyInfo> pros, string nodeName) { List <System.Reflection.PropertyInfo> result = new List <System.Reflection.PropertyInfo>(); foreach (var info in pros) { object[] custattributes = info.GetCustomAttributes(typeof(RequsetNode), true); if (custattributes != null && custattributes.Count() > 0) { RequsetNode n = custattributes[0] as RequsetNode; if (n != null && n.Name == nodeName && !string.IsNullOrEmpty(n.AttrName)) { result.Add(info); } } } return(result); }
public static void ParseCustomAttributes(List <System.Reflection.PropertyInfo> pros, StringBuilder xml, object sender, bool sort = false) { if (sort) { pros = pros.OrderBy(c => { int flag = 10000; object[] custattributes = c.GetCustomAttributes(typeof(RequsetNode), true); if (custattributes != null && custattributes.Count() > 0) { RequsetNode n = custattributes[0] as RequsetNode; if (n != null) { return(n.Sort); } } return(flag); }).ToList(); } for (int i = 0; i < pros.Count; i++) { System.Reflection.PropertyInfo info = pros[i]; object[] custattributes = info.GetCustomAttributes(typeof(RequsetNode), true); if (custattributes != null && custattributes.Count() > 0) { RequsetNode n = custattributes[0] as RequsetNode; if (n != null) { if (info.PropertyType.IsClass && !info.PropertyType.Name.Equals("String")) //是类 { if (info.PropertyType.IsArray || info.PropertyType.IsGenericType) { var valList = info.GetValue(sender, null) as System.Collections.IEnumerable; if (valList == null) { continue; } foreach (object valItem in valList) { if (valItem.GetType().IsClass&& !valItem.GetType().Name.Equals("String")) { System.Reflection.PropertyInfo[] lps = valItem.GetType().GetProperties(); if (lps != null && lps.Count() > 0) { var children = lps.ToList(); var attrsNodes = GetAttributes(children, n.Name); if (attrsNodes != null && attrsNodes.Count > 0) { ////找出同一个node中的其它attribute //var nodes = GetAttributes(pros, n.Name); string temp = string.Format("<{0} ", n.Name); temp += GetAttributeStr(attrsNodes, valItem, sort); temp += ">"; xml.Append(temp); //删除已作attribute的属性,此游标下的属性除外 children.RemoveAll(c => attrsNodes.Contains(c)); ParseCustomAttributes(children, xml, valItem, sort); xml.AppendFormat("</{0}>", n.Name); } else { xml.AppendFormat("<{0}>", n.Name); ParseCustomAttributes(children, xml, valItem, sort); xml.AppendFormat("</{0}>", n.Name); } } } else { xml.Append(string.Format("<{0}>{1}</{0}>", n.Name, valItem.ToString())); } } } else { object tvp = info.GetValue(sender, null); if (tvp != null) { System.Reflection.PropertyInfo[] ls = tvp.GetType().GetProperties(); if (ls != null && ls.Count() > 0) { var children = ls.ToList(); var attrsNodes = GetAttributes(children, n.Name); if (attrsNodes != null && attrsNodes.Count > 0) { ////找出同一个node中的其它attribute //var nodes = GetAttributes(pros, n.Name); string temp = string.Format("<{0} ", n.Name); temp += GetAttributeStr(attrsNodes, tvp, sort); temp += ">"; xml.Append(temp); //删除已作attribute的属性,此游标下的属性除外 children.RemoveAll(c => attrsNodes.Contains(c)); ParseCustomAttributes(children, xml, tvp, sort); xml.AppendFormat("</{0}>", n.Name); } else { xml.AppendFormat("<{0}>", n.Name); ParseCustomAttributes(children, xml, tvp, sort); xml.AppendFormat("</{0}>", n.Name); } } } else { if (!n.AllowEmpty) { throw new Exception(string.Format("属性{0}值不能为空!", info.Name)); } } } } else { object val = info.GetValue(sender, null); if (!n.AllowEmpty)//不允许空 { if (val == null || (val != null && string.IsNullOrEmpty(val.ToString()))) { throw new Exception(string.Format("属性{0}值不能为空!", info.Name)); } } if (val != null && !string.IsNullOrEmpty(val.ToString())) { if (!string.IsNullOrEmpty(n.AttrName)) { //找出同一个node中的其它attribute var nodes = GetAttributes(pros, n.Name); string temp = string.Format("<{0} ", n.Name); temp += GetAttributeStr(nodes, sender, sort); temp += string.Format("/>", n.Name); xml.Append(temp); //删除已作attribute的属性,此游标下的属性除外 pros.RemoveAll(c => nodes.Contains(c) && c != info); } else { xml.Append(string.Format("<{0}>{1}</{0}>", n.Name, val.ToString())); } } } } } } }