Dynamic property with value

public static class PropertyCopier<TParent, TChild> where TParent : class
                                            where TChild : class
        {
            public static void Copy(TParent parent, TChild child,int programID)
            {
                //int programID = -1;

                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 (typeof(ProgramFieldModel).GetField(parentProperty.Name + "Visible", BindingFlags.Public | BindingFlags.Static) != null)
                            {
                                FieldInfo field = typeof(ProgramFieldModel).GetField(parentProperty.Name + "Visible", BindingFlags.Public | BindingFlags.Static);
                                List<int> lookup = (List<int>)field.GetValue(null);
                                if(lookup.Contains(programID))
                                {
                                    childProperty.SetValue(child, parentProperty.GetValue(parent));
                                }
                            }
                           
                            //if (parentProperty.GetType().GetProperty(parentProperty.Name) != null)
                            //{
                            //    //ProgramFieldModel.C1801_RecognizedCredentialAttainedDateVisible.Contains(Convert.ToInt32(Model.ProgramID))
                            //    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