/// <summary> /// 对象实体 解析成get 的 XMLObject /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public static Dictionary <string, XMLObject> XmlToObjects_get <T>() { Dictionary <string, XMLObject> dic; if (xmlObjecDicCache.TryGetValue(typeof(T).AssemblyQualifiedName, out dic)) { return(dic); } dic = new Dictionary <string, XMLObject>(); Dictionary <string, Clazz> clazzDic = ReflectUtils.MethodToFunction <T>(); foreach (string methodName in clazzDic.Keys) { Clazz clazz = clazzDic[methodName]; MethodInfo m = clazz.GetMethodInfo; if (m == null) { continue; } XMLObject xmlObj = new XMLObject(); xmlObj.Key = methodName; xmlObj.MethodInfos.Add(m); xmlObj.MethoedName.Add(m.Name); xmlObj.Parameters.Add(null); xmlObj.Types.Add(null); dic.Add(methodName, xmlObj); } xmlObjecDicCache.Add(typeof(T).AssemblyQualifiedName, dic); return(dic); }
public static object GetObjectMethodResult(XMLObject xmlObject, object obj) { IList <String> ms = xmlObject.MethoedName; IList <MethodInfo> methodInfos = xmlObject.MethodInfos; Object result = null; if (methodInfos.Count > 0) { result = GetObjMethodValue(obj, methodInfos[0], xmlObject.Parameters[0]); for (int a = 1; a < ms.Count; a++) { if (result == null) { continue; } result = GetObjMethodValue(result, methodInfos[a], xmlObject.Parameters[a]); } } if (result == null) { return(xmlObject.Deafult); } else if (result is string) { string resultStr = (string)result; if ("".Equals(resultStr)) { return(xmlObject.Deafult); } else { return(resultStr); } } else { return(result); } }
public static Dictionary <String, XMLObject> XmlToObjects(String xmlPath, bool methodAll = true) { Dictionary <String, XMLObject> dic; if (xmlObjecDicCache.TryGetValue(xmlPath, out dic)) { return(dic); } dic = new Dictionary <String, XMLObject>(); XmlDocument xmlDoc = GetXmlDocument(xmlPath); XmlNodeList nodes = xmlDoc.GetElementsByTagName("property"); XmlNodeList v = xmlDoc.GetElementsByTagName("propertys"); string defaltClassType = null; string defualtMehtod = null; if (v.Count > 0) { defaltClassType = ((v.Item(0)) as XmlElement).GetAttribute("ClassType"); defualtMehtod = ((v.Item(0)) as XmlElement).GetAttribute("method"); } XMLObject obj; IList <String> methods; IList <object[]> parameterList; IList <Type[]> types; IList <MethodInfo> methodInfos; foreach (XmlNode node in nodes) { methods = new List <string>(); parameterList = new List <object[]>(); types = new List <Type[]>(); obj = new XMLObject(); methodInfos = new List <MethodInfo>(); obj.MethoedName = methods; obj.Parameters = parameterList; obj.Types = types; obj.MethodInfos = methodInfos; XmlElement xe = (XmlElement)node; if (xe.HasAttribute("key")) { obj.Key = xe.GetAttribute("key"); } else { obj.Key = xe.GetAttribute("name"); } if (xe.HasAttribute("column")) { obj.Column = xe.GetAttribute("column"); } obj.Deafult = xe.GetAttribute("default"); NodeValueToXmlObject(defaltClassType, xe, methods, methodInfos, parameterList, types); dic.Add(obj.Key, obj); } //添加还未加入的方法,确保所有get方法都能使用 if (methodAll) { Dictionary <string, Clazz> clazzDic = ReflectUtils.MethodToFunction(defaltClassType); foreach (string methodName in clazzDic.Keys) { if (!dic.ContainsKey(methodName)) { Clazz clazz = clazzDic[methodName]; MethodInfo m; if ("set_".Equals(defualtMehtod)) { m = clazz.SetMethodInfo; } else { m = clazz.GetMethodInfo; } if (m == null) { continue; } XMLObject xmlObj = new XMLObject(); xmlObj.Key = methodName; xmlObj.MethodInfos.Add(m); xmlObj.MethoedName.Add(m.Name); xmlObj.Parameters.Add(null); xmlObj.Types.Add(null); dic.Add(methodName, xmlObj); } } } //得到表格对象 //XmlNodeList tables = xmlDoc.GetElementsByTagName("table"); if (xmlObjecDicCache.ContainsKey(xmlPath)) { xmlObjecDicCache[xmlPath] = dic; } else { xmlObjecDicCache.Add(xmlPath, dic); } return(dic); }