/// <summary> /// 返回一个类的所有字段 /// </summary> /// <param name="t"></param> /// <returns></returns> public static string GetTypeField(Type t) { StringBuilder sb = new StringBuilder("["); foreach (FieldInfo fi in t.GetFields()) { object[] o = fi.GetCustomAttributes(true); if (o.Length <= 0) { continue; } SYSField sf = o[0] as SYSField; if (fi.FieldType.IsGenericType) { continue; } sb.Append("{\"finame\":\""); sb.Append(fi.Name); sb.Append("\",\"length\":\""); sb.Append(sf.Length); sb.Append("\",\"isnumberic\":\""); sb.Append(sf.IsNumberic ? "1" : "0"); sb.Append("\",\"blankmsg\":\""); sb.Append(sf.BlankMessage); sb.Append("\"},"); } sb.Remove(sb.Length - 1, 1); sb.Append("]"); return(sb.ToString()); }
/// <summary> /// 获取WFM接口实例对应的SAP实例 /// </summary> /// <returns></returns> public static Dictionary <string, object> GetSAPObject(object obj, out string sapclass) { sapclass = string.Empty; Type t = obj.GetType(); //获取类的对应SAP类 if (t.GetCustomAttributes(true).Length > 0) { ServiceClass sc = t.GetCustomAttributes(true)[0] as ServiceClass; sapclass = sc.SAPClass; } Dictionary <string, object> dic = new Dictionary <string, object>(); foreach (FieldInfo fi in t.GetFields()) { //获取属性 object[] o = fi.GetCustomAttributes(true); if (o.Length > 0) { //获取字段的SAP字段 SYSField sf = o[0] as SYSField; if (sf.SAPField == string.Empty) { continue; } if (fi.GetValue(obj) != null) { dic.Add(sf.SAPField, fi.GetValue(obj)); } } } return(dic); }
/// <summary> /// 获取BPM接口实例对应的SAP实例 /// </summary> /// <returns></returns> public static object GetRespObject(object obj, string targetname) { DataSet ds = new DataSet(); Type t = obj.GetType(); MemoryStream stream = new MemoryStream(); XmlSerializer xml = new XmlSerializer(t); xml.Serialize(stream, obj); stream.Position = 0; StreamReader sr = new StreamReader(stream, Encoding.UTF8); string strxml = sr.ReadToEnd(); sr.Close(); stream.Close(); XmlDocument xd = new XmlDocument(); xd.LoadXml(strxml); string nodename = t.Name.ToString().Replace("[]", string.Empty); if (obj is Array || strxml.Contains("COM_HEADER")) { //创建类型 Type ttype = Type.GetType(targetname); //创建实例 Array resps = null; string nnm = "ArrayOf" + nodename; if (strxml.Contains("COM_HEADER")) { nnm = nodename; } XmlNodeList nodes = xd.SelectNodes(nnm); int arrlength = nodes[0].ChildNodes.Count; int index = 0; foreach (XmlNode n in nodes[0].ChildNodes) { if (n.Name == "COM_HEADER") { arrlength--; continue; } Dictionary <string, string> dic = n.ChildNodes.Cast <XmlNode>().ToDictionary(it => it.Name, it => it.InnerText); if (resps == null) { resps = Array.CreateInstance(ttype, arrlength); } //创建实例 object resp = Activator.CreateInstance(ttype); foreach (FieldInfo fi in ttype.GetFields()) { //获取属性 object[] o = fi.GetCustomAttributes(true); if (o.Length <= 0) { continue; } //获取字段的SAP字段 SYSField sf = o[0] as SYSField; if (dic.ContainsKey(sf.SAPField)) { fi.SetValue(resp, dic[sf.SAPField]); } } resps.SetValue(resp, index); index++; } return(resps); } else { XmlNodeList nodes = xd.SelectNodes(nodename); Dictionary <string, string> dic = (from XmlNode node in nodes from XmlNode n in node.ChildNodes select n).ToDictionary(n => n.Name, n => n.InnerText); //创建类型 Type ttype = Type.GetType(targetname); //创建实例 object resp = Activator.CreateInstance(ttype); foreach (FieldInfo fi in ttype.GetFields()) { //获取属性 object[] o = fi.GetCustomAttributes(true); if (o.Length <= 0) { continue; } //获取字段的SAP字段 SYSField sf = o[0] as SYSField; if (dic.ContainsKey(sf.SAPField)) { fi.SetValue(resp, dic[sf.SAPField]); } } return(resp); } }