mirror of
https://github.com/mRemoteNG/mRemoteNG.git
synced 2026-02-25 19:38:37 +08:00
enum, int, bool -> string was throwing an exception. now using a simpler strategy for type conversion resolves #295
57 lines
2.6 KiB
C#
57 lines
2.6 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using mRemoteNG.App;
|
|
|
|
|
|
namespace mRemoteNG.Connection
|
|
{
|
|
public class DefaultConnectionInfo : ConnectionInfo
|
|
{
|
|
public static DefaultConnectionInfo Instance { get; } = new DefaultConnectionInfo();
|
|
private readonly string[] _excludedProperties = { "Parent", "Name", "Panel", "Hostname", "Port", "Inheritance",
|
|
"OpenConnections", "IsContainer", "IsDefault", "PositionID", "ConstantID", "TreeNode", "IsQuickConnect", "PleaseConnect" };
|
|
|
|
private DefaultConnectionInfo()
|
|
{
|
|
IsDefault = true;
|
|
}
|
|
|
|
public void LoadFrom<TSource>(TSource sourceInstance, Func<string, string> propertyNameMutator = null)
|
|
{
|
|
if (propertyNameMutator == null) propertyNameMutator = (a) => a;
|
|
var connectionProperties = GetProperties(_excludedProperties);
|
|
foreach (var property in connectionProperties)
|
|
{
|
|
var propertyFromSource = typeof(TSource).GetProperty(propertyNameMutator(property.Name));
|
|
var valueFromSource = propertyFromSource.GetValue(sourceInstance, null);
|
|
|
|
var descriptor = TypeDescriptor.GetProperties(Instance)[property.Name];
|
|
var converter = descriptor.Converter;
|
|
if (converter != null && converter.CanConvertFrom(valueFromSource.GetType()))
|
|
property.SetValue(Instance, converter.ConvertFrom(valueFromSource), null);
|
|
else
|
|
property.SetValue(Instance, valueFromSource, null);
|
|
}
|
|
}
|
|
|
|
public void SaveTo<TDestination>(TDestination destinationInstance, Func<string, string> propertyNameMutator = null)
|
|
{
|
|
if (propertyNameMutator == null) propertyNameMutator = (a) => a;
|
|
var inheritanceProperties = GetProperties(_excludedProperties);
|
|
foreach (var property in inheritanceProperties)
|
|
{
|
|
try
|
|
{
|
|
var propertyFromDestination = typeof(TDestination).GetProperty(propertyNameMutator(property.Name));
|
|
var localValue = property.GetValue(Instance, null);
|
|
var convertedValue = Convert.ChangeType(localValue, propertyFromDestination.PropertyType);
|
|
propertyFromDestination.SetValue(destinationInstance, convertedValue, null);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Runtime.MessageCollector?.AddExceptionStackTrace($"Error saving default connectioninfo property {property.Name}", ex);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |