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;
}
}
}
}
}
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
Post a Comment