/* * Greenshot - a free and open source screenshot tool * Copyright (C) 2007-2021 Thomas Braun, Jens Klingen, Robin Krom * * For more information see: http://getgreenshot.org/ * The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; namespace Greenshot.Base.Core { /// /// Extension methods which work for objects /// public static class ObjectExtensions { /// /// Perform a deep Copy of the object. /// /// The type of object being copied. /// The object instance to copy. /// The copied object. public static T Clone(this T source) { var typeparam = typeof(T); if (!typeparam.IsInterface && !typeparam.IsSerializable) { throw new ArgumentException("The type must be serializable.", nameof(source)); } // Don't serialize a null object, simply return the default for that object if (source == null) { return default; } IFormatter formatter = new BinaryFormatter(); using var stream = new MemoryStream(); formatter.Serialize(stream, source); stream.Seek(0, SeekOrigin.Begin); return (T) formatter.Deserialize(stream); } /// /// Clone the content from source to destination /// /// Type to clone /// Instance to copy from /// Instance to copy to public static void CloneTo(this T source, T destination) { var type = typeof(T); var myObjectFields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (var fieldInfo in myObjectFields) { fieldInfo.SetValue(destination, fieldInfo.GetValue(source)); } var myObjectProperties = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (var propertyInfo in myObjectProperties) { if (propertyInfo.CanWrite) { propertyInfo.SetValue(destination, propertyInfo.GetValue(source, null), null); } } } /// /// Compare two lists /// /// /// IList /// IList /// true if they are the same public static bool CompareLists(IList l1, IList l2) { if (l1.Count != l2.Count) { return false; } int matched = 0; foreach (T item in l1) { if (!l2.Contains(item)) { return false; } matched++; } return matched == l1.Count; } } }