Files
mRemoteNG/mRemoteV1/Connection/DefaultConnectionInheritance.cs
David Sparer edda670b4f refactored the config window property grid logic
Extracted the mapping of properties to the protocols that use them. We can now
declare the mapping directly within the AbstractConnectionInfo class. Created
a new class to handle the specific way we are using the FilteredPropertyGrid
to show connection info / inheritance data.
2019-04-01 13:24:05 -05:00

63 lines
2.5 KiB
C#

using System;
using System.ComponentModel;
using mRemoteNG.App;
namespace mRemoteNG.Connection
{
public class DefaultConnectionInheritance : ConnectionInfoInheritance
{
[Browsable(false)]
public static DefaultConnectionInheritance Instance { get; } = new DefaultConnectionInheritance();
private DefaultConnectionInheritance() : base(null, true)
{
}
static DefaultConnectionInheritance()
{
}
public void LoadFrom<TSource>(TSource sourceInstance, Func<string, string> propertyNameMutator = null)
{
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);
}
}
public void SaveTo<TDestination>(TDestination destinationInstance,
Func<string, string> propertyNameMutator = null)
{
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);
}
}
}
}