code clean up / null checks & logging

This commit is contained in:
Sean Kaim
2017-09-15 13:58:32 -04:00
parent 41fe211aec
commit d15e444cb7
2 changed files with 29 additions and 4 deletions

View File

@@ -18,11 +18,17 @@ namespace mRemoteNG.Connection
public void LoadFrom<TSource>(TSource sourceInstance, Func<string, string> propertyNameMutator = null)
{
if (propertyNameMutator == null) propertyNameMutator = (a) => a;
if (propertyNameMutator == null) propertyNameMutator = a => a;
var connectionProperties = GetProperties(_excludedProperties);
foreach (var property in connectionProperties)
{
var propertyFromSource = typeof(TSource).GetProperty(propertyNameMutator(property.Name));
if (propertyFromSource == null)
{
Runtime.MessageCollector.AddMessage(Messages.MessageClass.ErrorMsg,
$"DefaultConInfo-LoadFrom: Could not load {property.Name}", true);
continue;
}
var valueFromSource = propertyFromSource.GetValue(sourceInstance, null);
var descriptor = TypeDescriptor.GetProperties(Instance)[property.Name];
@@ -36,7 +42,7 @@ namespace mRemoteNG.Connection
public void SaveTo<TDestination>(TDestination destinationInstance, Func<string, string> propertyNameMutator = null)
{
if (propertyNameMutator == null) propertyNameMutator = (a) => a;
if (propertyNameMutator == null) propertyNameMutator = a => a;
var inheritanceProperties = GetProperties(_excludedProperties);
foreach (var property in inheritanceProperties)
{
@@ -44,6 +50,12 @@ namespace mRemoteNG.Connection
{
var propertyFromDestination = typeof(TDestination).GetProperty(propertyNameMutator(property.Name));
var localValue = property.GetValue(Instance, null);
if (propertyFromDestination == null)
{
Runtime.MessageCollector?.AddMessage(Messages.MessageClass.ErrorMsg,
$"DefaultConInfo-SaveTo: Could not load {property.Name}", true);
continue;
}
var convertedValue = Convert.ChangeType(localValue, propertyFromDestination.PropertyType);
propertyFromDestination.SetValue(destinationInstance, convertedValue, null);
}

View File

@@ -1,4 +1,5 @@
using System;
using mRemoteNG.App;
namespace mRemoteNG.Connection
@@ -17,11 +18,17 @@ namespace mRemoteNG.Connection
public void LoadFrom<TSource>(TSource sourceInstance, Func<string,string> propertyNameMutator = null)
{
if (propertyNameMutator == null) propertyNameMutator = (a) => a;
if (propertyNameMutator == null) propertyNameMutator = a => a;
var inheritanceProperties = GetProperties();
foreach (var property in inheritanceProperties)
{
var propertyFromSettings = typeof(TSource).GetProperty(propertyNameMutator(property.Name));
if (propertyFromSettings == null)
{
Runtime.MessageCollector.AddMessage(Messages.MessageClass.ErrorMsg,
$"DefaultConInherit-LoadFrom: Could not load {property.Name}", true);
continue;
}
var valueFromSettings = propertyFromSettings.GetValue(sourceInstance, null);
property.SetValue(Instance, valueFromSettings, null);
}
@@ -29,12 +36,18 @@ namespace mRemoteNG.Connection
public void SaveTo<TDestination>(TDestination destinationInstance, Func<string, string> propertyNameMutator = null)
{
if (propertyNameMutator == null) propertyNameMutator = (a) => a;
if (propertyNameMutator == null) propertyNameMutator = a => a;
var inheritanceProperties = GetProperties();
foreach (var property in inheritanceProperties)
{
var propertyFromSettings = typeof(TDestination).GetProperty(propertyNameMutator(property.Name));
var localValue = property.GetValue(Instance, null);
if (propertyFromSettings == null)
{
Runtime.MessageCollector?.AddMessage(Messages.MessageClass.ErrorMsg,
$"DefaultConInherit-SaveTo: Could not load {property.Name}", true);
continue;
}
propertyFromSettings.SetValue(destinationInstance, localValue, null);
}
}