mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 12:58:33 +01:00
added object copying to MLEM.Data
This commit is contained in:
parent
d2f38e9fbf
commit
ba24707b18
2 changed files with 46 additions and 1 deletions
37
MLEM.Data/CopyExtensions.cs
Normal file
37
MLEM.Data/CopyExtensions.cs
Normal file
|
@ -0,0 +1,37 @@
|
|||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace MLEM.Data {
|
||||
/// <summary>
|
||||
/// A set of extensions for dealing with copying objects.
|
||||
/// </summary>
|
||||
public static class CopyExtensions {
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="obj">The object to create a shallow copy of</param>
|
||||
/// <param name="flags">The binding flags for field searching</param>
|
||||
/// <typeparam name="T">The type of the object to copy</typeparam>
|
||||
/// <returns>A shallow copy of the object</returns>
|
||||
public static T Copy<T>(this T obj, BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) {
|
||||
var copy = (T) typeof(T).GetConstructor(Type.EmptyTypes).Invoke(null);
|
||||
obj.CopyInto(copy, flags);
|
||||
return copy;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies the given object <paramref name="obj"/> into the given object <see cref="otherObj"/>.
|
||||
/// </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>
|
||||
/// <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) {
|
||||
foreach (var field in typeof(T).GetFields(flags))
|
||||
field.SetValue(otherObj, field.GetValue(obj));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -96,6 +96,14 @@ namespace Sandbox {
|
|||
RectangleF = new RectangleF(4, 5, 6, 7).ToMlem(),
|
||||
Dir = Direction2.Left
|
||||
};
|
||||
Console.WriteLine(obj);
|
||||
|
||||
var copy = obj.Copy();
|
||||
Console.WriteLine(copy);
|
||||
|
||||
var intoCopy = new Test();
|
||||
obj.CopyInto(intoCopy);
|
||||
Console.WriteLine(intoCopy);
|
||||
|
||||
var writer = new StringWriter();
|
||||
this.Content.GetJsonSerializer().Serialize(writer, obj);
|
||||
|
@ -208,7 +216,7 @@ namespace Sandbox {
|
|||
public Point Point;
|
||||
public Rectangle Rectangle;
|
||||
public MLEM.Misc.RectangleF RectangleF;
|
||||
public Direction2 Dir;
|
||||
public Direction2 Dir { get; set; }
|
||||
|
||||
public override string ToString() {
|
||||
return $"{nameof(this.Vec)}: {this.Vec}, {nameof(this.Point)}: {this.Point}, {nameof(this.Rectangle)}: {this.Rectangle}, {nameof(this.RectangleF)}: {this.RectangleF}, {nameof(this.Dir)}: {this.Dir}";
|
||||
|
|
Loading…
Reference in a new issue