2020-07-31 19:07:22 +02:00
using System ;
2021-08-05 03:59:14 +02:00
using System.Collections.Generic ;
2020-10-25 20:11:15 +01:00
using System.Linq ;
2020-07-31 19:07:22 +02:00
using System.Reflection ;
2022-10-31 18:33:53 +01:00
#if NET6_0_OR_GREATER
using System.Diagnostics.CodeAnalysis ;
#endif
2020-07-31 19:07:22 +02:00
namespace MLEM.Data {
/// <summary>
/// A set of extensions for dealing with copying objects.
/// </summary>
2022-01-23 21:18:13 +01:00
[Obsolete("CopyExtensions has major flaws and insufficient speed compared to other libraries specifically designed for copying objects.")]
2022-12-13 13:11:36 +01:00
#if NET6_0_OR_GREATER
2022-10-31 18:33:53 +01:00
[UnconditionalSuppressMessage("Aot", "IL3050"), UnconditionalSuppressMessage("Aot", "IL2070"), UnconditionalSuppressMessage("Aot", "IL2090")]
2022-12-13 13:11:36 +01:00
#endif
2020-07-31 19:07:22 +02:00
public static class CopyExtensions {
2020-08-01 00:22:29 +02:00
private const BindingFlags DefaultFlags = BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic ;
2021-08-05 03:59:14 +02:00
private static readonly Dictionary < Type , ConstructorInfo > ConstructorCache = new Dictionary < Type , ConstructorInfo > ( ) ;
2020-08-01 00:22:29 +02:00
2020-07-31 19:07:22 +02:00
/// <summary>
/// Creates a shallow copy of the object and returns it.
2022-10-31 18:33:53 +01:00
/// Object creation occurs using a constructor with the <see cref="CopyConstructorAttribute"/> or, if none is present, the first constructor with the correct <see cref="BindingFlags"/>.
2020-07-31 19:07:22 +02:00
/// </summary>
/// <param name="obj">The object to create a shallow copy of</param>
/// <param name="flags">The binding flags for field searching</param>
2020-08-01 00:22:29 +02:00
/// <param name="fieldInclusion">A predicate that determines whether or not the given field should be copied. If null, all fields will be copied.</param>
2020-07-31 19:07:22 +02:00
/// <typeparam name="T">The type of the object to copy</typeparam>
/// <returns>A shallow copy of the object</returns>
2022-01-23 21:18:13 +01:00
[Obsolete("CopyExtensions has major flaws and insufficient speed compared to other libraries specifically designed for copying objects.")]
2022-06-15 11:38:11 +02:00
public static T Copy < T > ( this T obj , BindingFlags flags = CopyExtensions . DefaultFlags , Predicate < FieldInfo > fieldInclusion = null ) {
var copy = ( T ) CopyExtensions . Construct ( typeof ( T ) , flags ) ;
2020-08-01 00:22:29 +02:00
obj . CopyInto ( copy , flags , fieldInclusion ) ;
2020-07-31 19:07:22 +02:00
return copy ;
}
/// <summary>
2020-07-31 20:24:59 +02:00
/// Creates a deep copy of the object and returns it.
2022-10-31 18:33:53 +01:00
/// Object creation occurs using a constructor with the <see cref="CopyConstructorAttribute"/> or, if none is present, the first constructor with the correct <see cref="BindingFlags"/>.
2020-07-31 20:24:59 +02:00
/// </summary>
/// <param name="obj">The object to create a deep copy of</param>
/// <param name="flags">The binding flags for field searching</param>
2020-08-01 00:22:29 +02:00
/// <param name="fieldInclusion">A predicate that determines whether or not the given field should be copied. If null, all fields will be copied.</param>
2020-07-31 20:24:59 +02:00
/// <typeparam name="T">The type of the object to copy</typeparam>
/// <returns>A deep copy of the object</returns>
2022-01-23 21:18:13 +01:00
[Obsolete("CopyExtensions has major flaws and insufficient speed compared to other libraries specifically designed for copying objects.")]
2022-06-15 11:38:11 +02:00
public static T DeepCopy < T > ( this T obj , BindingFlags flags = CopyExtensions . DefaultFlags , Predicate < FieldInfo > fieldInclusion = null ) {
var copy = ( T ) CopyExtensions . Construct ( typeof ( T ) , flags ) ;
2020-08-01 00:22:29 +02:00
obj . DeepCopyInto ( copy , flags , fieldInclusion ) ;
2020-07-31 20:24:59 +02:00
return copy ;
}
/// <summary>
2020-10-06 20:14:57 +02:00
/// Copies the given object <paramref name="obj"/> into the given object <paramref name="otherObj"/> in a shallow manner.
2020-07-31 19:07:22 +02:00
/// </summary>
/// <param name="obj">The object to create a shallow copy of</param>
/// <param name="otherObj">The object to copy into</param>
/// <param name="flags">The binding flags for field searching</param>
2020-08-01 00:22:29 +02:00
/// <param name="fieldInclusion">A predicate that determines whether or not the given field should be copied. If null, all fields will be copied.</param>
2020-07-31 19:07:22 +02:00
/// <typeparam name="T">The type of the object to copy</typeparam>
2022-01-23 21:18:13 +01:00
[Obsolete("CopyExtensions has major flaws and insufficient speed compared to other libraries specifically designed for copying objects.")]
2022-06-15 11:38:11 +02:00
public static void CopyInto < T > ( this T obj , T otherObj , BindingFlags flags = CopyExtensions . DefaultFlags , Predicate < FieldInfo > fieldInclusion = null ) {
2020-08-01 00:22:29 +02:00
foreach ( var field in typeof ( T ) . GetFields ( flags ) ) {
if ( fieldInclusion = = null | | fieldInclusion ( field ) )
field . SetValue ( otherObj , field . GetValue ( obj ) ) ;
}
2020-07-31 19:07:22 +02:00
}
2020-07-31 20:24:59 +02:00
/// <summary>
2020-10-06 20:14:57 +02:00
/// Copies the given object <paramref name="obj"/> into the given object <paramref name="otherObj"/> in a deep manner.
2022-10-31 18:33:53 +01:00
/// Object creation occurs using a constructor with the <see cref="CopyConstructorAttribute"/> or, if none is present, the first constructor with the correct <see cref="BindingFlags"/>.
2020-07-31 20:24:59 +02:00
/// </summary>
/// <param name="obj">The object to create a deep copy of</param>
/// <param name="otherObj">The object to copy into</param>
/// <param name="flags">The binding flags for field searching</param>
2020-08-01 00:22:29 +02:00
/// <param name="fieldInclusion">A predicate that determines whether or not the given field should be copied. If null, all fields will be copied.</param>
2020-07-31 20:24:59 +02:00
/// <typeparam name="T">The type of the object to copy</typeparam>
2022-01-23 21:18:13 +01:00
[Obsolete("CopyExtensions has major flaws and insufficient speed compared to other libraries specifically designed for copying objects.")]
2022-06-15 11:38:11 +02:00
public static void DeepCopyInto < T > ( this T obj , T otherObj , BindingFlags flags = CopyExtensions . DefaultFlags , Predicate < FieldInfo > fieldInclusion = null ) {
2020-07-31 20:24:59 +02:00
foreach ( var field in obj . GetType ( ) . GetFields ( flags ) ) {
2020-08-01 00:22:29 +02:00
if ( fieldInclusion ! = null & & ! fieldInclusion ( field ) )
continue ;
2020-07-31 20:24:59 +02:00
var val = field . GetValue ( obj ) ;
2020-07-31 20:26:42 +02:00
if ( val = = null | | field . FieldType . IsValueType ) {
// if we're a value type (struct or primitive) or null, we can just set the value
2020-07-31 20:24:59 +02:00
field . SetValue ( otherObj , val ) ;
2020-07-31 20:26:42 +02:00
} else {
2020-07-31 20:24:59 +02:00
var otherVal = field . GetValue ( otherObj ) ;
// if the object we want to copy into doesn't have a value yet, we create one
if ( otherVal = = null ) {
2022-06-15 11:38:11 +02:00
otherVal = CopyExtensions . Construct ( field . FieldType , flags ) ;
2020-07-31 20:24:59 +02:00
field . SetValue ( otherObj , otherVal ) ;
}
val . DeepCopyInto ( otherVal , flags ) ;
}
}
}
private static object Construct ( Type t , BindingFlags flags ) {
2022-06-15 11:38:11 +02:00
if ( ! CopyExtensions . ConstructorCache . TryGetValue ( t , out var constructor ) ) {
2021-08-05 03:59:14 +02:00
var constructors = t . GetConstructors ( flags ) ;
// find a contructor with the correct attribute
constructor = constructors . FirstOrDefault ( c = > c . GetCustomAttribute < CopyConstructorAttribute > ( ) ! = null ) ;
// find a parameterless construcotr
if ( constructor = = null )
constructor = t . GetConstructor ( flags , null , Type . EmptyTypes , null ) ;
// fall back to the first constructor
if ( constructor = = null )
constructor = constructors . FirstOrDefault ( ) ;
if ( constructor = = null )
throw new NullReferenceException ( $"Type {t} does not have a constructor with the required visibility" ) ;
2022-06-15 11:38:11 +02:00
CopyExtensions . ConstructorCache . Add ( t , constructor ) ;
2021-08-05 03:59:14 +02:00
}
2020-10-25 20:11:15 +01:00
return constructor . Invoke ( new object [ constructor . GetParameters ( ) . Length ] ) ;
2020-07-31 20:24:59 +02:00
}
2020-07-31 19:07:22 +02:00
}
2020-10-25 20:11:15 +01:00
/// <summary>
/// An attribute that, when added to a constructor, will make that constructor the one used by <see cref="CopyExtensions.Copy{T}"/>, <see cref="CopyExtensions.DeepCopy{T}"/> and <see cref="CopyExtensions.DeepCopyInto{T}"/>.
/// </summary>
2022-01-23 21:18:13 +01:00
[AttributeUsage(AttributeTargets.Constructor), Obsolete("CopyExtensions has major flaws and insufficient speed compared to other libraries specifically designed for copying objects.")]
2021-12-28 14:56:11 +01:00
public class CopyConstructorAttribute : Attribute { }
2022-06-17 18:23:47 +02:00
}