mirror of
https://github.com/greenshot/greenshot
synced 2025-07-13 08:33:53 -07:00
Code formatting (indents etc.) to get it consistent, simplify it for contributors.
This commit is contained in:
parent
726644de99
commit
e8c0b307ee
435 changed files with 46647 additions and 39014 deletions
|
@ -26,79 +26,93 @@ using System.Reflection;
|
|||
using System.Runtime.Serialization;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
|
||||
namespace GreenshotPlugin.Core {
|
||||
/// <summary>
|
||||
/// Extension methods which work for objects
|
||||
/// </summary>
|
||||
public static class ObjectExtensions {
|
||||
|
||||
/// <summary>
|
||||
/// Perform a deep Copy of the object.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of object being copied.</typeparam>
|
||||
/// <param name="source">The object instance to copy.</param>
|
||||
/// <returns>The copied object.</returns>
|
||||
public static T Clone<T>(this T source)
|
||||
{
|
||||
var typeparam = typeof(T);
|
||||
if (!typeparam.IsInterface && !typeparam.IsSerializable)
|
||||
{
|
||||
throw new ArgumentException("The type must be serializable.", nameof(source));
|
||||
}
|
||||
namespace GreenshotPlugin.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods which work for objects
|
||||
/// </summary>
|
||||
public static class ObjectExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Perform a deep Copy of the object.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of object being copied.</typeparam>
|
||||
/// <param name="source">The object instance to copy.</param>
|
||||
/// <returns>The copied object.</returns>
|
||||
public static T Clone<T>(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();
|
||||
// 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);
|
||||
return (T) formatter.Deserialize(stream);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clone the content from source to destination
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type to clone</typeparam>
|
||||
/// <param name="source">Instance to copy from</param>
|
||||
/// <param name="destination">Instance to copy to</param>
|
||||
public static void CloneTo<T>(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);
|
||||
/// <summary>
|
||||
/// Clone the content from source to destination
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type to clone</typeparam>
|
||||
/// <param name="source">Instance to copy from</param>
|
||||
/// <param name="destination">Instance to copy to</param>
|
||||
public static void CloneTo<T>(this T source, T destination)
|
||||
{
|
||||
var type = typeof(T);
|
||||
var myObjectFields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
|
||||
|
||||
foreach (var propertyInfo in myObjectProperties) {
|
||||
if (propertyInfo.CanWrite) {
|
||||
propertyInfo.SetValue(destination, propertyInfo.GetValue(source, null), null);
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (var fieldInfo in myObjectFields)
|
||||
{
|
||||
fieldInfo.SetValue(destination, fieldInfo.GetValue(source));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compare two lists
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="l1">IList</param>
|
||||
/// <param name="l2">IList</param>
|
||||
/// <returns>true if they are the same</returns>
|
||||
public static bool CompareLists<T>(IList<T> l1, IList<T> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compare two lists
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="l1">IList</param>
|
||||
/// <param name="l2">IList</param>
|
||||
/// <returns>true if they are the same</returns>
|
||||
public static bool CompareLists<T>(IList<T> l1, IList<T> 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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue