mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-26 14:38:34 +01:00
Allow copying to exclude certain fields
This commit is contained in:
parent
0529e2c921
commit
3a1b73934b
1 changed files with 18 additions and 8 deletions
|
@ -7,17 +7,20 @@ namespace MLEM.Data {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class CopyExtensions {
|
public static class CopyExtensions {
|
||||||
|
|
||||||
|
private const BindingFlags DefaultFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a shallow copy of the object and returns it.
|
/// Creates a shallow copy of the object and returns it.
|
||||||
/// Note that, for this to work correctly, <typeparamref name="T"/> needs to contain a parameterless constructor.
|
/// Note that, for this to work correctly, <typeparamref name="T"/> needs to contain a parameterless constructor.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="obj">The object to create a shallow copy of</param>
|
/// <param name="obj">The object to create a shallow copy of</param>
|
||||||
/// <param name="flags">The binding flags for field searching</param>
|
/// <param name="flags">The binding flags for field searching</param>
|
||||||
|
/// <param name="fieldInclusion">A predicate that determines whether or not the given field should be copied. If null, all fields will be copied.</param>
|
||||||
/// <typeparam name="T">The type of the object to copy</typeparam>
|
/// <typeparam name="T">The type of the object to copy</typeparam>
|
||||||
/// <returns>A shallow copy of the object</returns>
|
/// <returns>A shallow copy of the object</returns>
|
||||||
public static T Copy<T>(this T obj, BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) {
|
public static T Copy<T>(this T obj, BindingFlags flags = DefaultFlags, Predicate<FieldInfo> fieldInclusion = null) {
|
||||||
var copy = (T) Construct(typeof(T), flags);
|
var copy = (T) Construct(typeof(T), flags);
|
||||||
obj.CopyInto(copy, flags);
|
obj.CopyInto(copy, flags, fieldInclusion);
|
||||||
return copy;
|
return copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,11 +30,12 @@ namespace MLEM.Data {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="obj">The object to create a deep copy of</param>
|
/// <param name="obj">The object to create a deep copy of</param>
|
||||||
/// <param name="flags">The binding flags for field searching</param>
|
/// <param name="flags">The binding flags for field searching</param>
|
||||||
|
/// <param name="fieldInclusion">A predicate that determines whether or not the given field should be copied. If null, all fields will be copied.</param>
|
||||||
/// <typeparam name="T">The type of the object to copy</typeparam>
|
/// <typeparam name="T">The type of the object to copy</typeparam>
|
||||||
/// <returns>A deep copy of the object</returns>
|
/// <returns>A deep copy of the object</returns>
|
||||||
public static T DeepCopy<T>(this T obj, BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) {
|
public static T DeepCopy<T>(this T obj, BindingFlags flags = DefaultFlags, Predicate<FieldInfo> fieldInclusion = null) {
|
||||||
var copy = (T) Construct(typeof(T), flags);
|
var copy = (T) Construct(typeof(T), flags);
|
||||||
obj.DeepCopyInto(copy, flags);
|
obj.DeepCopyInto(copy, flags, fieldInclusion);
|
||||||
return copy;
|
return copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,11 +45,14 @@ namespace MLEM.Data {
|
||||||
/// <param name="obj">The object to create a shallow copy of</param>
|
/// <param name="obj">The object to create a shallow copy of</param>
|
||||||
/// <param name="otherObj">The object to copy into</param>
|
/// <param name="otherObj">The object to copy into</param>
|
||||||
/// <param name="flags">The binding flags for field searching</param>
|
/// <param name="flags">The binding flags for field searching</param>
|
||||||
|
/// <param name="fieldInclusion">A predicate that determines whether or not the given field should be copied. If null, all fields will be copied.</param>
|
||||||
/// <typeparam name="T">The type of the object to copy</typeparam>
|
/// <typeparam name="T">The type of the object to copy</typeparam>
|
||||||
public static void CopyInto<T>(this T obj, T otherObj, BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) {
|
public static void CopyInto<T>(this T obj, T otherObj, BindingFlags flags = DefaultFlags, Predicate<FieldInfo> fieldInclusion = null) {
|
||||||
foreach (var field in typeof(T).GetFields(flags))
|
foreach (var field in typeof(T).GetFields(flags)) {
|
||||||
|
if (fieldInclusion == null || fieldInclusion(field))
|
||||||
field.SetValue(otherObj, field.GetValue(obj));
|
field.SetValue(otherObj, field.GetValue(obj));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Copies the given object <paramref name="obj"/> into the given object <see cref="otherObj"/> in a deep manner.
|
/// Copies the given object <paramref name="obj"/> into the given object <see cref="otherObj"/> in a deep manner.
|
||||||
|
@ -54,9 +61,12 @@ namespace MLEM.Data {
|
||||||
/// <param name="obj">The object to create a deep copy of</param>
|
/// <param name="obj">The object to create a deep copy of</param>
|
||||||
/// <param name="otherObj">The object to copy into</param>
|
/// <param name="otherObj">The object to copy into</param>
|
||||||
/// <param name="flags">The binding flags for field searching</param>
|
/// <param name="flags">The binding flags for field searching</param>
|
||||||
|
/// <param name="fieldInclusion">A predicate that determines whether or not the given field should be copied. If null, all fields will be copied.</param>
|
||||||
/// <typeparam name="T">The type of the object to copy</typeparam>
|
/// <typeparam name="T">The type of the object to copy</typeparam>
|
||||||
public static void DeepCopyInto<T>(this T obj, T otherObj, BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) {
|
public static void DeepCopyInto<T>(this T obj, T otherObj, BindingFlags flags = DefaultFlags, Predicate<FieldInfo> fieldInclusion = null) {
|
||||||
foreach (var field in obj.GetType().GetFields(flags)) {
|
foreach (var field in obj.GetType().GetFields(flags)) {
|
||||||
|
if (fieldInclusion != null && !fieldInclusion(field))
|
||||||
|
continue;
|
||||||
var val = field.GetValue(obj);
|
var val = field.GetValue(obj);
|
||||||
if (val == null || field.FieldType.IsValueType) {
|
if (val == null || field.FieldType.IsValueType) {
|
||||||
// if we're a value type (struct or primitive) or null, we can just set the value
|
// if we're a value type (struct or primitive) or null, we can just set the value
|
||||||
|
|
Loading…
Reference in a new issue