예제 #1
0
        /// <summary>
        /// 根据XML 节点设置实体对象的属性或者字段的值。
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="xmlNode"></param>
        public void FillEntityValue(object entity, XmlNode xmlNode)
        {
            if (entity == null)
            {
                return;
            }
            ModelXmlConfigAttribute att = Attribute.GetCustomAttribute(entity.GetType(), typeof(ModelXmlConfigAttribute)) as ModelXmlConfigAttribute;

            if (att == null)
            {
                return;
            }
            if (att.ByXmlNodeAttribute)
            {
                fillEntityValueByXmlAttribute(entity, xmlNode);
            }
            else
            {
                fillEntityByXmlInnerText(entity, xmlNode);
            }
        }
예제 #2
0
        //设置引用类型的值
        private void setRefrenceEntityValue(object entity, PropertyInfo info, PropertyXmlConfigAttribute att, XmlNode xmlNode)
        {
            //判断是否引用集合
            if (info.PropertyType.GetInterface("IList") != null)
            {
                if (att.ReferenceModelType == null)
                {
                    return;
                }

                object value = info.GetValue(entity, null);

                if (value == null)
                {
                    return;
                }

                IList       lstValue    = value as IList;
                XmlNodeList childsNodes = null;
                if (att.NotExistsGroupNode)
                {
                    childsNodes = xmlNode.ParentNode.ChildNodes;
                }
                else
                {
                    childsNodes = xmlNode.ChildNodes;
                }

                if (childsNodes.Count == 0)
                {
                    return;
                }

                string nodeName = string.IsNullOrEmpty(att.MappingName) ? info.Name : att.MappingName;
                foreach (XmlNode lstChild in childsNodes)
                {
                    if (lstChild.NodeType != XmlNodeType.Element)
                    {
                        continue;
                    }

                    // if (string.Compare(lstChild.Name, nodeName, true) != 0) continue;

                    object childEntity = DllFactory.Instance.CreateInstance(att.ReferenceModelType);
                    lstValue.Add(childEntity);
                    ModelXmlConfigAttribute t = Attribute.GetCustomAttribute(childEntity.GetType(), typeof(ModelXmlConfigAttribute)) as ModelXmlConfigAttribute;
                    if (t == null)
                    {
                        MB.Util.TraceEx.Write(string.Format("类型{0} 没有配置ModelXmlConfigAttribute。", childEntity.GetType().FullName));
                        continue;
                    }
                    if (t.ByXmlNodeAttribute)
                    {
                        fillEntityValueByXmlAttribute(childEntity, lstChild);
                    }
                    else
                    {
                        fillEntityByXmlInnerText(childEntity, lstChild);
                    }
                }
            }
            else
            {
                object childEntity = DllFactory.Instance.CreateInstance(info.PropertyType);

                MB.Util.MyReflection.Instance.InvokePropertyForSet(entity, info.Name, childEntity);

                ModelXmlConfigAttribute t = Attribute.GetCustomAttribute(childEntity.GetType(), typeof(ModelXmlConfigAttribute)) as ModelXmlConfigAttribute;
                if (t == null)
                {
                    MB.Util.TraceEx.Write(string.Format("类型{0} 没有配置ModelXmlConfigAttribute。", childEntity.GetType().FullName));
                    return;
                }

                if (t.ByXmlNodeAttribute)
                {
                    fillEntityValueByXmlAttribute(childEntity, xmlNode);
                }
                else
                {
                    fillEntityByXmlInnerText(childEntity, xmlNode);
                }
            }
        }