示例#1
0
        /// <summary>
        ///    Maps a property to an xml node.
        /// </summary>
        /// <param name="memberType">Type of member to map.</param>
        /// <param name="memberName">Name of member to map.</param>
        /// <returns>The property mapping.</returns>
        protected virtual XmlNodeMap Map(TypeMemberInfo.MemberType memberType, string memberName)
        {
            if (string.IsNullOrEmpty(memberName))
            {
                throw new ArgumentNullException("memberName");
            }
            Type objType = typeof(T);
            Type dataType;

            switch (memberType)
            {
            case TypeMemberInfo.MemberType.Field:
                var fld = objType.Field(memberName);
                dataType = fld.FieldType;
                break;

            case TypeMemberInfo.MemberType.Property:
                var prop = objType.Property(memberName);
                dataType = prop.PropertyType;
                break;

            default:
                throw new MappingException("Unknown member type");
            }

            var nodeMap = new XmlNodeMap(memberName, memberType, dataType);

            _Mappings.Add(nodeMap);
            return(nodeMap);
        }
示例#2
0
        /// <summary>
        ///    Maps a property to an xml node.
        /// </summary>
        /// <param name="expression">The property to map.</param>
        /// <returns>The property mapping.</returns>
        protected virtual XmlNodeMap Map(Expression <Func <T, object> > expression)
        {
            var        member = TypeHelper.GetMemberExpression(expression).Member;
            XmlNodeMap nodeMap;
            var        property = member as PropertyInfo;

            if (property != null)
            {
                nodeMap = new XmlNodeMap(property.Name, TypeMemberInfo.MemberType.Property, property.PropertyType);
                _Mappings.Add(nodeMap);
                return(nodeMap);
            }

            var field = member as FieldInfo;

            if (field != null)
            {
                nodeMap = new XmlNodeMap(field.Name, TypeMemberInfo.MemberType.Field, field.FieldType);
                _Mappings.Add(nodeMap);
                return(nodeMap);
            }

            throw new MappingException(string.Format("'{0}' is not a property or field reference.", member.Name));
        }