/// <summary>
/// Copies the value of all fields from one <see cref="object"/> to another.
/// </summary>
internal static void CopyTo <T>(this T from, T to) where T : class
{
// Find base type of both compilations
TypeInfo fromType = from.GetType().GetTypeInfo();
TypeInfo toType = to.GetType().GetTypeInfo();
Type baseType;
if (fromType.IsAssignableFrom(toType))
{
// ToCompilation inherits FromCompilation
baseType = fromType.AsType();
}
else if (toType.IsAssignableFrom(fromType))
{
// FromCompilation inherits ToCompilation
baseType = toType.AsType();
}
else
{
// No common type: find first common type
baseType = FindCommonType(fromType.AsType(), toType.AsType());
}
// Copy fields from one compilation to the other
foreach (FieldInfo field in baseType.GetAllFields())
{
if (field.IsStatic)
{
continue;
}
field.SetValue(to, field.GetValue(from));
}
}