Ejemplo n.º 1
0
        /// <summary>
        /// Set the specified property value of an object
        /// </summary>
        /// <param name="vo">the Value Object on which setting is to be performed</param>
        /// <param name="propertyName">Property name</param>
        /// <param name="propertyValue">Value to be set</param>
        public static Object SetProperty(object vo, string propertyName, Object propertyValue)
        {
            if (vo == null)
            {
                throw new System.ArgumentException("No object specified to set.");
            }

            if ((propertyName == null) || (propertyName.Length < 1))
            {
                throw new System.ArgumentException("No property specified to set.");
            }


            PropertyInfo propInfo = vo.GetType().GetProperty(propertyName);

            if (propInfo == null)
            {
                throw new System.ArgumentException("The class '" + vo.GetType().Name + "' does not have the property '" + propertyName + "'");
            }

            Object destValue;

            if (propInfo.PropertyType.IsEnum)
            {
                vo = PropertyHelper.SetEnumTypeProperty(vo, propInfo.Name, propertyValue.ToString(), true);
            }
            else
            {
                // Convert if necessary
                destValue = ConverterHelper.Convert(propertyValue, propInfo.PropertyType);


                // Set the value
                vo.GetType().InvokeMember(propInfo.Name,
                                          BindingFlags.SetProperty,
                                          null,
                                          vo,
                                          new object[] { destValue });
            }
            return(vo);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Copy property values from the origin value object (VO) to the destination VO
        /// for all cases where the property names are the same.  For each
        /// property, a conversion is attempted as necessary.
        ///
        /// All combinations of standard value objects (.Net Property) and
        /// javabean like value objects (property getters and setters) are supported.
        ///
        /// Properties that exist in the origin VO, but do not exist
        /// in the destination VO (or are read-only in the destination bean) are
        /// silently ignored.
        /// </summary>
        /// <remarks>
        ///		Note that the JavaBean VO standards mandates that the getters MUST have
        ///		the method names beginning with a lowercase 'get' and the setters names MUST
        ///		begin with a lowercase 'set'. Eg. getAddress() and setAddress() are
        ///		valid getter and setter names for the Address property.
        /// </remarks>
        /// <param name="srcObject">Value object to be copied from</param>
        /// <param name="destObject">Value object to be copied to</param>
        /// <returns>returned value object copied from original to destination</returns>
        public static object CopyProperties(object srcObject, object destObject)
        {
            // null objects cannot be reflected
            if (srcObject == null)
            {
                return(destObject);
            }

            // --------------------------------
            // copy public properties
            // --------------------------------
            foreach (PropertyInfo propInfoFromObj in srcObject.GetType().GetProperties())
            {
                PropertyInfo propInfoDestObj = destObject.GetType().GetProperty(propInfoFromObj.Name);

                // Ensure that the property exists in the destination object
                // and that it can be written, ie. it has the setter.
                if (propInfoDestObj == null || !propInfoDestObj.CanWrite)
                {
                    continue;
                }

                object srcValue = srcObject.GetType().InvokeMember(propInfoFromObj.Name,
                                                                   BindingFlags.GetProperty,
                                                                   null,
                                                                   srcObject,
                                                                   null);


                // Convert if necessary
                Object destValue = ConverterHelper.Convert(srcValue, propInfoDestObj.PropertyType);


                destObject.GetType().InvokeMember(propInfoDestObj.Name,
                                                  BindingFlags.SetProperty,
                                                  null,
                                                  destObject,
                                                  new object[] { destValue });
            }

            // --------------------------------
            // copy public fields
            // --------------------------------
            foreach (FieldInfo fieldInfoFromObj in srcObject.GetType().GetFields())
            {
                FieldInfo fieldInfoDestObj = destObject.GetType().GetField(fieldInfoFromObj.Name);

                // Ensure field exists in the destination object
                if (fieldInfoDestObj == null)
                {
                    continue;
                }

                Object srcValue = srcObject.GetType().InvokeMember(fieldInfoFromObj.Name,
                                                                   BindingFlags.GetField,
                                                                   null,
                                                                   srcObject,
                                                                   null);

                // Convert if necessary
                Object destValue = ConverterHelper.Convert(srcValue, fieldInfoDestObj.FieldType);


                destObject.GetType().InvokeMember(fieldInfoDestObj.Name,
                                                  BindingFlags.SetField,
                                                  null,
                                                  destObject,
                                                  new object[] { destValue });
            }

            // ------------------------------------------
            // copy getter and setter type properties
            // ------------------------------------------
            foreach (MethodInfo fromObjMethodInfo in srcObject.GetType().GetMethods())
            {
                if (fromObjMethodInfo.Name.StartsWith("get") && !fromObjMethodInfo.Name.StartsWith("get_"))
                {
                    string     setterMethodName  = "set" + fromObjMethodInfo.Name.Substring(3, fromObjMethodInfo.Name.Length - 3);
                    MethodInfo methodInfoDestobj = destObject.GetType().GetMethod(setterMethodName);

                    if (methodInfoDestobj != null)
                    {
                        try
                        {
                            object srcValue = srcObject
                                              .GetType()
                                              .InvokeMember(fromObjMethodInfo.Name,
                                                            BindingFlags.InvokeMethod,
                                                            null,
                                                            srcObject,
                                                            null);


                            destObject
                            .GetType()
                            .InvokeMember(methodInfoDestobj.Name,
                                          BindingFlags.InvokeMethod,
                                          null,
                                          destObject,
                                          new object[] { srcValue });
                        }
                        catch
                        {
                            //ignore and continue
                        }
                    }
                }
            }
            // return the updated object
            return(destObject);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 1 level Deep copying of an <see cref="System.Object"/> properties and
        /// fields to another.
        /// Both private and public fields will be copied.
        /// </summary>
        /// <param name="srcObject">Source <see cref="System.Object"/> to copy from</param>
        /// <param name="destObject">Destination <see cref="System.Object"/> to copy to</param>
        /// <returns>Copied destination object</returns>
        public static object Copy(object srcObject, object destObject)
        {
            // null objects cannot be reflected
            if (srcObject == null)
            {
                return(destObject);
            }

            // --------------------------------
            // copy public properties
            // --------------------------------
            foreach (PropertyInfo propInfoFromObj in srcObject.GetType().GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
            {
                PropertyInfo propInfoDestObj = destObject.GetType().GetProperty(propInfoFromObj.Name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

                if (propInfoDestObj != null)
                {
                    object srcValue = srcObject.GetType().InvokeMember(propInfoFromObj.Name,
                                                                       BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
                                                                       null,
                                                                       srcObject,
                                                                       null);


                    // Convert if necessary
                    Object destValue = ConverterHelper.Convert(srcValue, propInfoDestObj.PropertyType);


                    destObject.GetType().InvokeMember(propInfoDestObj.Name,
                                                      BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
                                                      null,
                                                      destObject,
                                                      new object[] { destValue });
                }
            }

            // --------------------------------
            // copy private & public fields
            // --------------------------------
            foreach (FieldInfo fieldInfoFromObj in srcObject.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
            {
                FieldInfo fieldInfoDestObj = destObject.GetType().GetField(fieldInfoFromObj.Name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

                if (fieldInfoDestObj != null)
                {
                    Object srcValue = srcObject.GetType().InvokeMember(fieldInfoFromObj.Name,
                                                                       BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
                                                                       null,
                                                                       srcObject,
                                                                       null);

                    // Convert if necessary
                    Object destValue = ConverterHelper.Convert(srcValue, fieldInfoDestObj.FieldType);


                    destObject.GetType().InvokeMember(fieldInfoDestObj.Name,
                                                      BindingFlags.SetField | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
                                                      null,
                                                      destObject,
                                                      new object[] { destValue });
                }
            }

            return(destObject);
        }