private static Dictionary <string, EntityToXmlMapper> InitEntityToXmlMapper <T>(T t, dynamic propertyGetDict, List <string> propertyList) where T : class { Dictionary <string, EntityToXmlMapper> mapperDict = new Dictionary <string, EntityToXmlMapper>(); string propertyValue = null; XmlTAttribute attribute = null; ReflectionGenericHelper.Foreach <T>((PropertyInfo propertyInfo) => { if (propertyList == null || propertyList.IndexOf(propertyInfo.Name) >= 0) { if (propertyGetDict != null && propertyGetDict.ContainsKey(propertyInfo.Name)) { propertyValue = propertyGetDict[propertyInfo.Name](t).ToString(); } else { propertyValue = ReflectionHelper.GetPropertyValue(t, propertyInfo).ToString(); } attribute = propertyInfo.GetCustomAttribute <XmlTAttribute>(); if (attribute != null) { if (!string.IsNullOrEmpty(attribute.Name)) { mapperDict.Add(attribute.Name, new EntityToXmlMapper() { Value = propertyValue, ValueEnum = attribute.NameType }); } else { mapperDict.Add(propertyInfo.Name, new EntityToXmlMapper() { Value = propertyValue, ValueEnum = attribute.NameType }); } } else { mapperDict.Add(propertyInfo.Name, new EntityToXmlMapper() { Value = propertyValue, ValueEnum = XmlTEnum.Attribute }); } } }); return(mapperDict); }
private static Dictionary <PropertyInfo, XmlTAttribute> InitXmlToEntityMapper(Type type) { string key = type.FullName; if (PropertyAttributeDict.ContainsKey(key)) { return(PropertyAttributeDict[key]); } lock (lockItem) { Dictionary <PropertyInfo, XmlTAttribute> propertyDict = new Dictionary <PropertyInfo, XmlTAttribute>(); XmlTAttribute attribute = null; ReflectionHelper.Foreach((PropertyInfo propertyInfo) => { attribute = propertyInfo.GetCustomAttribute <XmlTAttribute>(); if (attribute != null) { propertyDict.Add(propertyInfo, attribute); } else { if (ReflectionHelper.IsListType(propertyInfo.PropertyType)) { attribute = new XmlTAttribute(propertyInfo.Name, XmlTEnum.ElementList); } else if (ReflectionHelper.IsCustomType(propertyInfo.PropertyType)) { attribute = new XmlTAttribute(propertyInfo.Name, XmlTEnum.Element); } else { attribute = new XmlTAttribute(propertyInfo.Name, XmlTEnum.Attribute); } propertyDict.Add(propertyInfo, attribute); } }, type); if (!PropertyAttributeDict.ContainsKey(key)) { PropertyAttributeDict.Add(key, propertyDict); } return(propertyDict); } }