one class property value to another class propety c#

public class PropertyCopier<TParent, TChild> where TParent : class
                                            where TChild : class
    {
        public static void Copy(TParent parent, TChild child)
        {
            var parentProperties = parent.GetType().GetProperties();
            var childProperties = child.GetType().GetProperties();

            foreach (var parentProperty in parentProperties)
            {
                foreach (var childProperty in childProperties)
                {
                    if (parentProperty.Name != "ID" && childProperty.Name != "ID" && parentProperty.Name == childProperty.Name && parentProperty.PropertyType == childProperty.PropertyType)
                    {
                        if (parentProperty.GetValue(parent) != null)
                        {
                            childProperty.SetValue(child, parentProperty.GetValue(parent));
                        }
                        break;
                    }
                }
            }
        }
    }

Comments

Popular posts from this blog

SECURING WEBAPI USING JSON WEB TOKEN (JWT) IN WEB API C#

Adding ASP.NET MVC5 Identity Authentication to an existing project

Multiple model pass in MVC using tuple or Tuple in MVC