BUG-2021 & BUG-2022: Fixes for exceptions.

This commit is contained in:
Robin 2016-09-14 10:50:40 +02:00
parent 14086923df
commit 35ed3b8d60
6 changed files with 51 additions and 26 deletions

View file

@ -153,7 +153,7 @@ namespace Greenshot.Drawing.Fields
{ {
ClearFields(); ClearFields();
internalUpdateRunning = true; internalUpdateRunning = true;
foreach (Field field in FindCommonFields()) foreach (var field in FindCommonFields())
{ {
SetFieldValue(field.FieldType, field.Value); SetFieldValue(field.FieldType, field.Value);
} }

View file

@ -1910,7 +1910,7 @@ namespace Greenshot.Drawing
FieldAggregator.UnbindElement(container); FieldAggregator.UnbindElement(container);
if (generateEvents && _movingElementChanged != null) if (generateEvents && _movingElementChanged != null)
{ {
SurfaceElementEventArgs eventArgs = new SurfaceElementEventArgs {Elements = selectedElements}; var eventArgs = new SurfaceElementEventArgs {Elements = selectedElements};
_movingElementChanged(this, eventArgs); _movingElementChanged(this, eventArgs);
} }
} }

View file

@ -56,7 +56,8 @@ namespace Greenshot.Drawing
/// The StringFormat object is not serializable!! /// The StringFormat object is not serializable!!
/// </summary> /// </summary>
[NonSerialized] [NonSerialized]
StringFormat _stringFormat = new StringFormat(); private StringFormat _stringFormat = new StringFormat();
public StringFormat StringFormat => _stringFormat; public StringFormat StringFormat => _stringFormat;
// Although the name is wrong, we can't change it due to file serialization // 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; 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) if (_textBox.Visible)
{ {
_textBox.Invalidate(); _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) if (_textBox == null)
{ {
@ -467,7 +473,7 @@ namespace Greenshot.Drawing
_textBox.ForeColor = lineColor; _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 // 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)) 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 // next change will be made undoable
makeUndoable = true; makeUndoable = true;

View file

@ -80,7 +80,7 @@ namespace Greenshot.Memento
var other = otherMemento as DrawableContainerBoundsChangeMemento; var other = otherMemento as DrawableContainerBoundsChangeMemento;
if (other != null) if (other != null)
{ {
if (Objects.CompareLists<IDrawableContainer>(listOfdrawableContainer, other.listOfdrawableContainer)) if (ObjectExtensions.CompareLists<IDrawableContainer>(listOfdrawableContainer, other.listOfdrawableContainer))
{ {
// Lists are equal, as we have the state already we can ignore the new memento // Lists are equal, as we have the state already we can ignore the new memento
return true; return true;

View file

@ -18,6 +18,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -26,7 +27,10 @@ using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization.Formatters.Binary;
namespace GreenshotPlugin.Core { namespace GreenshotPlugin.Core {
public static class Objects { /// <summary>
/// Extension methods which work for objects
/// </summary>
public static class ObjectExtensions {
/// <summary> /// <summary>
/// Perform a deep Copy of the object. /// Perform a deep Copy of the object.
@ -34,41 +38,56 @@ namespace GreenshotPlugin.Core {
/// <typeparam name="T">The type of object being copied.</typeparam> /// <typeparam name="T">The type of object being copied.</typeparam>
/// <param name="source">The object instance to copy.</param> /// <param name="source">The object instance to copy.</param>
/// <returns>The copied object.</returns> /// <returns>The copied object.</returns>
public static T Clone<T>(this T source) { public static T Clone<T>(this T source)
if (!typeof(T).IsSerializable) { {
throw new ArgumentException("The type must be serializable.", "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 // Don't serialize a null object, simply return the default for that object
if (ReferenceEquals(source, null)) { if (source == null) {
return default(T); return default(T);
} }
IFormatter formatter = new BinaryFormatter(); IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream(); using (var stream = new MemoryStream()) {
using (stream) {
formatter.Serialize(stream, source); formatter.Serialize(stream, source);
stream.Seek(0, SeekOrigin.Begin); 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) { public static void CloneTo<T>(this T source, T destination) {
Type type = typeof(T); var type = typeof(T);
FieldInfo[] myObjectFields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); var myObjectFields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
foreach (FieldInfo fi in myObjectFields) { foreach (var fieldInfo in myObjectFields) {
fi.SetValue(destination, fi.GetValue(source)); 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) { foreach (var propertyInfo in myObjectProperties) {
if (pi.CanWrite) { if (propertyInfo.CanWrite) {
pi.SetValue(destination, pi.GetValue(source, null), null); 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) { public static bool CompareLists<T>(IList<T> l1, IList<T> l2) {
if (l1.Count != l2.Count) { if (l1.Count != l2.Count) {
return false; return false;

View file

@ -189,7 +189,7 @@
<Compile Include="Core\LogHelper.cs" /> <Compile Include="Core\LogHelper.cs" />
<Compile Include="Core\NetworkHelper.cs" /> <Compile Include="Core\NetworkHelper.cs" />
<Compile Include="Core\OAuthHelper.cs" /> <Compile Include="Core\OAuthHelper.cs" />
<Compile Include="Core\Objects.cs" /> <Compile Include="Core\ObjectExtensions.cs" />
<Compile Include="Core\PluginUtils.cs" /> <Compile Include="Core\PluginUtils.cs" />
<Compile Include="Core\QuantizerHelper.cs" /> <Compile Include="Core\QuantizerHelper.cs" />
<Compile Include="Core\RssHelper.cs" /> <Compile Include="Core\RssHelper.cs" />