locked
Method to copy properties on one method to another. RRS feed

  • Question

  •  Hi All,

    This is the method to copy all the properties of one object to another.

    Scenario is, to replace the properties of object in list with the properties of another.

    Suggest me if anything is better than this. 

    public static void CopyProperties(object objSource, object objDestination)
            {
                try
                {
                    //get the list of all properties in the destination object
                    var destProps = objDestination.GetType().GetProperties();

                    //get the list of all properties in the source object
                    foreach (var sourceProp in objSource.GetType().GetProperties())
                    {
                        foreach (var destProperty in destProps)
                        {
                            //if we find match between source & destination properties name, set
                            //the value to the destination property
                            if (destProperty.Name == sourceProp.Name && destProperty.PropertyType.IsAssignableFrom(sourceProp.PropertyType))
                            {
                                destProperty.SetValue(objDestination, sourceProp.GetValue(objSource, null), null);
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {

                }
            }

                   
    Friday, July 27, 2012 11:56 AM

Answers

  •     class myClass

            {

                public string Name { get; set; }

                public string Surname { get; set; }

              

            }

         private static void CopyObj()

            {

                myClass source = new myClass { Name = "Sergio", Surname = "Russo" };

                myClass destination = new myClass();

                foreach (PropertyInfo p in source.GetType().GetProperties())

                {

                    try

                    {

                        if ((p.CanRead) && (p.CanWrite))

                        {

                            var o = p.GetValue(source, null);

                            if (o != null)

                            {

                                if (o is Guid)

                                {

                                    if (((Guid)o) != Guid.Empty)

                                        p.SetValue(destination, o, null);

                                }

                                else

                                {

                                    p.SetValue(destination, o, null);

                                }

                            }

                        }

                    }

                    catch (Exception)

                    {

                        //Manage exception

                        throw;

                    }

                }

            }


    Regards,
    Bubu
    http://zsvipullo.blogspot.it

    Friday, July 27, 2012 2:34 PM