Example #1
0
        public void MapObject(Type typeOfObjectToMap, MappingMethods mappingMethods)
        {
            mappingMethods.Start();

            MapObject(null, mappingMethods, typeOfObjectToMap);

            mappingMethods.End();
        }
Example #2
0
        public void MapObject(Type typeOfObjectToMap, MappingMethods mappingMethods)
        {
            mappingMethods.Start();

            MapObject(null, mappingMethods, typeOfObjectToMap);

            mappingMethods.End();
        }
Example #3
0
        // This isn't being used,
        // so removing it to clear
        // out a warning.
        //MappingMethods mMappingMethods;

        #endregion

        #region Methods

        #region XML Docs
        /// <summary>
        /// Pass an object and a MappingMethods and MapObject will map each member in that object to the corresponding
        /// method from the mappingMethods.
        /// </summary>
        /// <param name="objectToMap">object whose members will be called on.</param>
        /// <param name="mappingMethods">MappingMethods which has dictionaries of members/methods.</param>
        #endregion
        public void MapObject(object objectToMap, MappingMethods mappingMethods)
        {
            mappingMethods.Start();

            if (objectToMap == null)
            {
                throw new ArgumentException("The objectToMap is a null type");
            }
            else
            {
                MapObject(objectToMap, mappingMethods, objectToMap.GetType());
            }

            mappingMethods.End();
        }
Example #4
0
        // This isn't being used, 
        // so removing it to clear
        // out a warning.
        //MappingMethods mMappingMethods;

        #endregion

        #region Methods

        #region XML Docs
        /// <summary>
        /// Pass an object and a MappingMethods and MapObject will map each member in that object to the corresponding
        /// method from the mappingMethods.
        /// </summary>
        /// <param name="objectToMap">object whose members will be called on.</param>
        /// <param name="mappingMethods">MappingMethods which has dictionaries of members/methods.</param>
        #endregion
        public void MapObject(object objectToMap, MappingMethods mappingMethods)
        {
            mappingMethods.Start();

            if (objectToMap == null)
            {
                throw new ArgumentException("The objectToMap is a null type");
            }
            else
            {
                MapObject(objectToMap, mappingMethods, objectToMap.GetType());
            }

            mappingMethods.End();
        }
Example #5
0
        public void MapObject(object objectToMap, MappingMethods mappingMethods, Type typeOfArgumentObject)
        {
            // get all your properties you want to map
            // put them in the right order
            List <MemberInfo> members = GetMembers(typeOfArgumentObject,
                                                   mappingMethods.AcceptedMemberTypes, mappingMethods.MemberBitMask);

            #region Get rid of same type recursions; REVERSE FOR LOOP YAH!
            for (int i = members.Count - 1; i > -1; i--)
            {
                if (members[i].MemberType == MemberTypes.Field)
                {
                    if ((members[i] as FieldInfo).FieldType == typeOfArgumentObject)
                    {
                        members.Remove(members[i]);
                    }
                }
                else if (members[i].MemberType == MemberTypes.Property)
                {
                    if ((members[i] as PropertyInfo).PropertyType == typeOfArgumentObject)
                    {
                        members.Remove(members[i]);
                    }
                }
            }
            #endregion

            foreach (MemberInfo memberInfo in members)
            {
                object memberValue = null;
                //Type typeOfMember;

                #region Map Property
                if (memberInfo.MemberType == MemberTypes.Property)
                {
                    if (objectToMap != null)
                    {
                        memberValue = (memberInfo as PropertyInfo).GetValue(objectToMap, null);
                    }

                    MapMember(memberInfo, mappingMethods, memberValue);
                }
                #endregion

                #region Map Field
                else if (memberInfo.MemberType == MemberTypes.Field)
                {
                    if (objectToMap != null)
                    {
                        memberValue = (memberInfo as FieldInfo).GetValue(objectToMap);
                    }

                    MapMember(memberInfo, mappingMethods, memberValue);
                }
                #endregion

                else //Not a Property or Field, ERROR! (this should never happen)
                {
                    throw new Exception("MemberType not accepted, MapObject only accepts Fields and Properties.");
                }
            }
        }
Example #6
0
        private void MapMember(MemberInfo member, MappingMethods mappingMethods, object value)
        {
            Type typeOfMember;

            #region PropertyInfo
            if (member.MemberType == MemberTypes.Property)
            {
                typeOfMember = (member as PropertyInfo).PropertyType;
                if (mappingMethods.HasMethodForType(typeOfMember))
                {
                    MemberMapping methodToCall = mappingMethods.GetMappingForType(typeOfMember);

                    methodToCall(member as PropertyInfo, value);
                }
                else if (mappingMethods.BreakdownUnknownTypes == false)
                {
                    throw new ArgumentException("ObjectMapper found a type to map that it cannot handle," +
                                                " fix this by adding a Mapping to your MappingMethods class");
                }
                else if (typeOfMember.IsPrimitive == false)
                {
                    if (value == null)
                    {
                        value = System.Activator.CreateInstance((member as PropertyInfo).PropertyType);
                    }
                    this.MapObject(value, mappingMethods, typeOfMember);
                }
            }
            #endregion

            #region FieldInfo
            else if (member.MemberType == MemberTypes.Field)
            {
                typeOfMember = (member as FieldInfo).FieldType;
                if (mappingMethods.HasMethodForType(typeOfMember))
                {
                    MemberMapping methodToCall = mappingMethods.GetMappingForType(typeOfMember);

                    methodToCall(member as FieldInfo, value);
                }
                else if (mappingMethods.BreakdownUnknownTypes == false)
                {
                    throw new ArgumentException("ObjectMapper found a type to map that it cannot handle," +
                                                " fix this by adding a Mapping to your MappingMethods class");
                }
                else if (typeOfMember.IsPrimitive == false)
                {
                    if (value == null)
                    {
                        value = System.Activator.CreateInstance((member as FieldInfo).FieldType);
                    }
                    this.MapObject(value, mappingMethods, typeOfMember);
                }
            }
            #endregion

            else //Not a Property or Field, ERROR! (this should never happen)
            {
                throw new Exception("MemberType not accepted, MapObject only accepts Fields and Properties.");
            }
        }
Example #7
0
        public void MapObject(object objectToMap, MappingMethods mappingMethods, Type typeOfArgumentObject)
        {
            // get all your properties you want to map
            // put them in the right order
            List<MemberInfo> members = GetMembers(typeOfArgumentObject,
                mappingMethods.AcceptedMemberTypes, mappingMethods.MemberBitMask);

            #region Get rid of same type recursions; REVERSE FOR LOOP YAH!
            for (int i = members.Count - 1; i > -1; i--)
            {
                if (members[i].MemberType == MemberTypes.Field)
                {
                    if ((members[i] as FieldInfo).FieldType == typeOfArgumentObject)
                    {
                        members.Remove(members[i]);
                    }
                }
                else if (members[i].MemberType == MemberTypes.Property)
                {
                    if ((members[i] as PropertyInfo).PropertyType == typeOfArgumentObject)
                    {
                        members.Remove(members[i]);
                    }
                }
            }
            #endregion

            foreach (MemberInfo memberInfo in members)
            {
                object memberValue = null;
                //Type typeOfMember;

                #region Map Property
                if (memberInfo.MemberType == MemberTypes.Property)
                {
                    if (objectToMap != null)
                    {
                        memberValue = (memberInfo as PropertyInfo).GetValue(objectToMap, null);
                    }

                    MapMember(memberInfo, mappingMethods, memberValue);
                }
                #endregion

                #region Map Field
                else if (memberInfo.MemberType == MemberTypes.Field)
                {
                    if (objectToMap != null)
                    {
                        memberValue = (memberInfo as FieldInfo).GetValue(objectToMap);
                    }

                    MapMember(memberInfo, mappingMethods, memberValue);
                }
                #endregion

                else //Not a Property or Field, ERROR! (this should never happen)
                {
                    throw new Exception("MemberType not accepted, MapObject only accepts Fields and Properties.");
                }

            }

        }
Example #8
0
        private void MapMember(MemberInfo member, MappingMethods mappingMethods, object value)
        {
            Type typeOfMember;

            #region PropertyInfo
            if (member.MemberType == MemberTypes.Property)
            {
                typeOfMember = (member as PropertyInfo).PropertyType;
                if (mappingMethods.HasMethodForType(typeOfMember))
                {
                    MemberMapping methodToCall = mappingMethods.GetMappingForType(typeOfMember);

                    methodToCall(member as PropertyInfo, value);
                }
                else if (mappingMethods.BreakdownUnknownTypes == false)
                {
                    throw new ArgumentException("ObjectMapper found a type to map that it cannot handle," +
                        " fix this by adding a Mapping to your MappingMethods class");
                }
                else if (typeOfMember.IsPrimitive == false)
                {
                    if (value == null)
                    {
                        value = System.Activator.CreateInstance((member as PropertyInfo).PropertyType);
                    }
                    this.MapObject(value, mappingMethods, typeOfMember);
                }
            }
            #endregion

            #region FieldInfo
            else if (member.MemberType == MemberTypes.Field)
            {
                typeOfMember = (member as FieldInfo).FieldType;
                if (mappingMethods.HasMethodForType(typeOfMember))
                {
                    MemberMapping methodToCall = mappingMethods.GetMappingForType(typeOfMember);

                    methodToCall(member as FieldInfo, value);
                }
                else if (mappingMethods.BreakdownUnknownTypes == false)
                {
                    throw new ArgumentException("ObjectMapper found a type to map that it cannot handle," +
                        " fix this by adding a Mapping to your MappingMethods class");
                }
                else if (typeOfMember.IsPrimitive == false)
                {
                    if (value == null)
                    {
                        value = System.Activator.CreateInstance((member as FieldInfo).FieldType);
                    }
                    this.MapObject(value, mappingMethods, typeOfMember);
                }
            }
            #endregion

            else //Not a Property or Field, ERROR! (this should never happen)
            {
                throw new Exception("MemberType not accepted, MapObject only accepts Fields and Properties.");
            }

        }