diff --git a/Greenshot/Drawing/Fields/FieldAggregator.cs b/Greenshot/Drawing/Fields/FieldAggregator.cs index e9ba4b282..0bbd395d3 100644 --- a/Greenshot/Drawing/Fields/FieldAggregator.cs +++ b/Greenshot/Drawing/Fields/FieldAggregator.cs @@ -153,7 +153,7 @@ namespace Greenshot.Drawing.Fields { ClearFields(); internalUpdateRunning = true; - foreach (Field field in FindCommonFields()) + foreach (var field in FindCommonFields()) { SetFieldValue(field.FieldType, field.Value); } diff --git a/Greenshot/Drawing/Surface.cs b/Greenshot/Drawing/Surface.cs index 12199733d..cc727eb7b 100644 --- a/Greenshot/Drawing/Surface.cs +++ b/Greenshot/Drawing/Surface.cs @@ -1910,7 +1910,7 @@ namespace Greenshot.Drawing FieldAggregator.UnbindElement(container); if (generateEvents && _movingElementChanged != null) { - SurfaceElementEventArgs eventArgs = new SurfaceElementEventArgs {Elements = selectedElements}; + var eventArgs = new SurfaceElementEventArgs {Elements = selectedElements}; _movingElementChanged(this, eventArgs); } } diff --git a/Greenshot/Drawing/TextContainer.cs b/Greenshot/Drawing/TextContainer.cs index 18a3e66f6..84deb9868 100644 --- a/Greenshot/Drawing/TextContainer.cs +++ b/Greenshot/Drawing/TextContainer.cs @@ -56,7 +56,8 @@ namespace Greenshot.Drawing /// The StringFormat object is not serializable!! /// [NonSerialized] - StringFormat _stringFormat = new StringFormat(); + private StringFormat _stringFormat = new StringFormat(); + public StringFormat StringFormat => _stringFormat; // Although the name is wrong, we can't change it due to file serialization @@ -172,8 +173,13 @@ namespace Greenshot.Drawing Height = textSize.Height + lineThickness; } - void TextContainer_PropertyChanged(object sender, PropertyChangedEventArgs e) + private void TextContainer_PropertyChanged(object sender, PropertyChangedEventArgs e) { + if (_textBox == null) + { + return; + } + if (_textBox.Visible) { _textBox.Invalidate(); @@ -203,7 +209,7 @@ namespace Greenshot.Drawing } } - void TextContainer_FieldChanged(object sender, FieldChangedEventArgs e) + private void TextContainer_FieldChanged(object sender, FieldChangedEventArgs e) { if (_textBox == null) { @@ -467,7 +473,7 @@ namespace Greenshot.Drawing _textBox.ForeColor = lineColor; } - void textBox_KeyDown(object sender, KeyEventArgs e) + private void textBox_KeyDown(object sender, KeyEventArgs e) { // ESC and Enter/Return (w/o Shift) hide text editor if (e.KeyCode == Keys.Escape || ((e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter) && e.Modifiers == Keys.None)) @@ -477,7 +483,7 @@ namespace Greenshot.Drawing } } - void textBox_LostFocus(object sender, EventArgs e) + private void textBox_LostFocus(object sender, EventArgs e) { // next change will be made undoable makeUndoable = true; diff --git a/Greenshot/Memento/DrawableContainerBoundsChangeMemento.cs b/Greenshot/Memento/DrawableContainerBoundsChangeMemento.cs index 48b94e91c..37564b694 100644 --- a/Greenshot/Memento/DrawableContainerBoundsChangeMemento.cs +++ b/Greenshot/Memento/DrawableContainerBoundsChangeMemento.cs @@ -80,7 +80,7 @@ namespace Greenshot.Memento var other = otherMemento as DrawableContainerBoundsChangeMemento; if (other != null) { - if (Objects.CompareLists(listOfdrawableContainer, other.listOfdrawableContainer)) + if (ObjectExtensions.CompareLists(listOfdrawableContainer, other.listOfdrawableContainer)) { // Lists are equal, as we have the state already we can ignore the new memento return true; diff --git a/GreenshotPlugin/Core/Objects.cs b/GreenshotPlugin/Core/ObjectExtensions.cs similarity index 60% rename from GreenshotPlugin/Core/Objects.cs rename to GreenshotPlugin/Core/ObjectExtensions.cs index 8a56ea73c..0e31fd5d5 100644 --- a/GreenshotPlugin/Core/Objects.cs +++ b/GreenshotPlugin/Core/ObjectExtensions.cs @@ -18,6 +18,7 @@ * 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; @@ -26,7 +27,10 @@ using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; namespace GreenshotPlugin.Core { - public static class Objects { + /// + /// Extension methods which work for objects + /// + public static class ObjectExtensions { /// /// Perform a deep Copy of the object. @@ -34,41 +38,56 @@ namespace GreenshotPlugin.Core { /// The type of object being copied. /// The object instance to copy. /// The copied object. - public static T Clone(this T source) { - if (!typeof(T).IsSerializable) { - throw new ArgumentException("The type must be serializable.", "source"); + 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 (ReferenceEquals(source, null)) { + if (source == null) { return default(T); } IFormatter formatter = new BinaryFormatter(); - Stream stream = new MemoryStream(); - using (stream) { + 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) { - Type type = typeof(T); - FieldInfo[] myObjectFields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); + var type = typeof(T); + var myObjectFields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); - foreach (FieldInfo fi in myObjectFields) { - fi.SetValue(destination, fi.GetValue(source)); + foreach (var fieldInfo in myObjectFields) { + fieldInfo.SetValue(destination, fieldInfo.GetValue(source)); } - PropertyInfo[] myObjectProperties = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); + var myObjectProperties = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); - foreach (PropertyInfo pi in myObjectProperties) { - if (pi.CanWrite) { - pi.SetValue(destination, pi.GetValue(source, null), null); + 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; diff --git a/GreenshotPlugin/GreenshotPlugin.csproj b/GreenshotPlugin/GreenshotPlugin.csproj index b26c5c405..608c8a10b 100644 --- a/GreenshotPlugin/GreenshotPlugin.csproj +++ b/GreenshotPlugin/GreenshotPlugin.csproj @@ -189,7 +189,7 @@ - +