1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-27 16:20:10 +00:00
msfs-popout-panel-manager/Shared/ObjectExtension.cs

154 lines
5.5 KiB
C#
Raw Normal View History

2022-01-27 13:40:04 +00:00
using MSFSPopoutPanelManager.Shared.ArrayExtensions;
using System;
using System.Collections.Generic;
2022-07-23 19:23:32 +00:00
using System.Collections.ObjectModel;
using System.Linq;
2022-01-27 13:40:04 +00:00
using System.Reflection;
namespace MSFSPopoutPanelManager.Shared
{
public static class ObjectExtensions
{
2024-02-28 02:44:21 +00:00
private static readonly MethodInfo CloneMethod = typeof(object).GetMethod("MemberwiseClone", BindingFlags.NonPublic | BindingFlags.Instance);
2022-01-27 13:40:04 +00:00
public static bool IsPrimitive(this Type type)
{
if (type == typeof(String)) return true;
return (type.IsValueType & type.IsPrimitive);
}
2024-02-28 02:44:21 +00:00
public static object Copy(this object originalObject)
2022-01-27 13:40:04 +00:00
{
2024-02-28 02:44:21 +00:00
return InternalCopy(originalObject, new Dictionary<object, object>(new ReferenceEqualityComparer()));
2022-01-27 13:40:04 +00:00
}
2022-06-30 23:53:08 +00:00
2024-02-28 02:44:21 +00:00
private static object InternalCopy(object originalObject, IDictionary<object, object> visited)
2022-01-27 13:40:04 +00:00
{
2022-06-30 23:53:08 +00:00
if (originalObject == null)
return null;
2022-01-27 13:40:04 +00:00
var typeToReflect = originalObject.GetType();
2022-06-30 23:53:08 +00:00
if (IsPrimitive(typeToReflect))
return originalObject;
2024-02-28 02:44:21 +00:00
if (visited.TryGetValue(originalObject, out var copy))
return copy;
2022-06-30 23:53:08 +00:00
if (typeof(Delegate).IsAssignableFrom(typeToReflect))
return null;
2022-01-27 13:40:04 +00:00
var cloneObject = CloneMethod.Invoke(originalObject, null);
if (typeToReflect.IsArray)
{
var arrayType = typeToReflect.GetElementType();
if (IsPrimitive(arrayType) == false)
{
2024-02-28 02:44:21 +00:00
var clonedArray = (Array)cloneObject;
clonedArray?.ForEach((array, indices) => array.SetValue(InternalCopy(clonedArray.GetValue(indices), visited), indices));
2022-01-27 13:40:04 +00:00
}
}
visited.Add(originalObject, cloneObject);
CopyFields(originalObject, visited, cloneObject, typeToReflect);
RecursiveCopyBaseTypePrivateFields(originalObject, visited, cloneObject, typeToReflect);
return cloneObject;
}
private static void RecursiveCopyBaseTypePrivateFields(object originalObject, IDictionary<object, object> visited, object cloneObject, Type typeToReflect)
{
if (typeToReflect.BaseType != null)
{
RecursiveCopyBaseTypePrivateFields(originalObject, visited, cloneObject, typeToReflect.BaseType);
CopyFields(originalObject, visited, cloneObject, typeToReflect.BaseType, BindingFlags.Instance | BindingFlags.NonPublic, info => info.IsPrivate);
}
}
private static void CopyFields(object originalObject, IDictionary<object, object> visited, object cloneObject, Type typeToReflect, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy, Func<FieldInfo, bool> filter = null)
{
foreach (FieldInfo fieldInfo in typeToReflect.GetFields(bindingFlags))
{
if (filter != null && filter(fieldInfo) == false) continue;
if (IsPrimitive(fieldInfo.FieldType)) continue;
var originalFieldValue = fieldInfo.GetValue(originalObject);
var clonedFieldValue = InternalCopy(originalFieldValue, visited);
fieldInfo.SetValue(cloneObject, clonedFieldValue);
}
}
2022-06-30 23:53:08 +00:00
2022-01-27 13:40:04 +00:00
public static T Copy<T>(this T original)
{
2024-02-28 02:44:21 +00:00
return (T)Copy((object)original);
2022-01-27 13:40:04 +00:00
}
}
2022-07-23 19:23:32 +00:00
public static class ObservableCollectionExtensionMethods
{
public static int RemoveAll<T>(this ObservableCollection<T> observableCollection, Func<T, bool> condition)
{
// Find all elements satisfying the condition, i.e. that will be removed
var toRemove = observableCollection.Where(condition).ToList();
return toRemove.Count(observableCollection.Remove);
}
}
2024-02-28 02:44:21 +00:00
public class ReferenceEqualityComparer : EqualityComparer<object>
2022-01-27 13:40:04 +00:00
{
public override bool Equals(object x, object y)
{
return ReferenceEquals(x, y);
}
2022-06-30 23:53:08 +00:00
2022-01-27 13:40:04 +00:00
public override int GetHashCode(object obj)
{
return obj.GetHashCode();
}
}
namespace ArrayExtensions
{
public static class ArrayExtensions
{
public static void ForEach(this Array array, Action<Array, int[]> action)
{
if (array.LongLength == 0) return;
2024-02-28 02:44:21 +00:00
var walker = new ArrayTraverse(array);
2022-01-27 13:40:04 +00:00
do action(array, walker.Position);
while (walker.Step());
}
}
internal class ArrayTraverse
{
public int[] Position;
2024-02-28 02:44:21 +00:00
private readonly int[] _maxLengths;
2022-01-27 13:40:04 +00:00
public ArrayTraverse(Array array)
{
2024-02-28 02:44:21 +00:00
_maxLengths = new int[array.Rank];
for (var i = 0; i < array.Rank; ++i)
2022-01-27 13:40:04 +00:00
{
2024-02-28 02:44:21 +00:00
_maxLengths[i] = array.GetLength(i) - 1;
2022-01-27 13:40:04 +00:00
}
Position = new int[array.Rank];
}
public bool Step()
{
2024-02-28 02:44:21 +00:00
for (var i = 0; i < Position.Length; ++i)
2022-01-27 13:40:04 +00:00
{
2024-02-28 02:44:21 +00:00
if (Position[i] >= _maxLengths[i])
continue;
Position[i]++;
for (var j = 0; j < i; j++)
2022-01-27 13:40:04 +00:00
{
2024-02-28 02:44:21 +00:00
Position[j] = 0;
2022-01-27 13:40:04 +00:00
}
2024-02-28 02:44:21 +00:00
return true;
2022-01-27 13:40:04 +00:00
}
return false;
}
}
}
}