mirror of
https://github.com/mRemoteNG/mRemoteNG.git
synced 2026-02-17 22:11:48 +08:00
code clean up / null checks & logging
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user