Esempio n. 1
0
        /// <summary>
        /// Set a field value if the field exists and is accessible in the object
        /// </summary>
        /// <param name="fieldName">Name of the field </param>
        /// <param name="fieldValue">Value to set the field to</param>
        /// <param name="srcObject">the object to inspect</param>
        /// <param name="bSetPrivate">flag to indicate if field to set is a private field</param>
        /// <returns></returns>
        public static object SetField(string fieldName, object fieldValue, object srcObject, bool bSetPrivate)
        {
            BindingFlags bindingflags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;

            if (bSetPrivate)
            {
                bindingflags = bindingflags | BindingFlags.NonPublic;
            }

            if (Exists(fieldName, srcObject, bSetPrivate))
            {
                FieldInfo fieldInfo = srcObject.GetType().GetField(fieldName, bindingflags);

                if (fieldInfo != null)
                {
                    // Convert if necessary
                    Object destValue = ConverterHelper.Convert(fieldValue, fieldInfo.FieldType);

                    // Set the value
                    srcObject.GetType().InvokeMember(fieldInfo.Name,
                                                     BindingFlags.SetField | bindingflags,
                                                     null,
                                                     srcObject,
                                                     new object[] { destValue });
                }
            }

            return(srcObject);
        }
Esempio n. 2
0
        /// <summary>
        /// Copy property values from the origin value object (VO) to the destination VO.
        /// </summary>
        /// <param name="propertyName">Name of the property to copy from</param>
        /// <param name="srcObj">source object to copy from</param>
        /// <param name="destObj">destination object to copy to</param>
        /// <returns>returned value object copied</returns>
        /// <exception cref="System.ArgumentNullException">thrown if either the source
        /// or the destination objects are not supplied.</exception>
        /// <exception cref="System.ArgumentException">thrown if the property
        /// to be copied does not exists in the source
        /// </exception>
        public static object CopyProperty(string propertyName, object srcObj, object destObj)
        {
            if (srcObj == null)
            {
                throw new System.ArgumentNullException("srcObj");
            }

            if (destObj == null)
            {
                throw new System.ArgumentNullException("destObj");
            }

            if ((propertyName == null) || (propertyName.Length < 1))
            {
                throw new System.ArgumentNullException("propertyName is null or empty");
            }

            PropertyInfo propInfoSrcObj = srcObj.GetType().GetProperty(propertyName);

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

            PropertyInfo propInfoDestObj = destObj.GetType().GetProperty(propertyName);

            if (propInfoDestObj == null || !propInfoDestObj.CanWrite)
            {
                throw new System.ArgumentException("The class '" + destObj.GetType().Name + "' does not have the property '" + propertyName + "' or the property cannot be set.");
            }

            // Get the value from property.
            object srcValue = srcObj.GetType()
                              .InvokeMember(propInfoSrcObj.Name,
                                            BindingFlags.GetProperty,
                                            null,
                                            srcObj,
                                            null);

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


            // Set the value
            destObj.GetType().InvokeMember(propInfoDestObj.Name,
                                           BindingFlags.SetProperty,
                                           null,
                                           destObj,
                                           new object[] { destValue });

            return(destObj);
        }
Esempio n. 3
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);
        }
Esempio n. 4
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);
        }
Esempio n. 5
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);
        }