Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

C# Reflection copy every property of a class to another class with the same properties.
 

Let's say you have to copy all the values of the properties of a class to another (maybe larger) class, but there's no inheritance between them.
You can achieve this using reflection very simply.

foreach (var prop in item.GetType().GetProperties())
{
    var p2 = outputModel.GetType().GetProperty(prop.Name);
    p2.SetValue(outputModel, prop.GetValue(item, null), null);
}