|
|
|
@ -5,6 +5,7 @@ |
|
|
|
// All rights reserved. Licensed under the MIT license.
|
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
using System.ComponentModel; |
|
|
|
using System.Globalization; |
|
|
|
using Squidex.Infrastructure.Reflection.Internal; |
|
|
|
|
|
|
|
@ -66,6 +67,41 @@ namespace Squidex.Infrastructure.Reflection |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private sealed class TypeConverterPropertyMapper : PropertyMapper |
|
|
|
{ |
|
|
|
private readonly TypeConverter converter; |
|
|
|
|
|
|
|
public TypeConverterPropertyMapper( |
|
|
|
PropertyAccessor sourceAccessor, |
|
|
|
PropertyAccessor targetAccessor, |
|
|
|
TypeConverter converter) |
|
|
|
: base(sourceAccessor, targetAccessor) |
|
|
|
{ |
|
|
|
this.converter = converter; |
|
|
|
} |
|
|
|
|
|
|
|
public override void MapProperty(object source, object target, CultureInfo culture) |
|
|
|
{ |
|
|
|
var value = GetValue(source); |
|
|
|
|
|
|
|
if (value == null) |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
var converted = converter.ConvertFrom(null, culture, value); |
|
|
|
|
|
|
|
SetValue(target, converted); |
|
|
|
} |
|
|
|
catch |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private class PropertyMapper |
|
|
|
{ |
|
|
|
private readonly PropertyAccessor sourceAccessor; |
|
|
|
@ -126,24 +162,36 @@ namespace Squidex.Infrastructure.Reflection |
|
|
|
if (sourceType == targetType) |
|
|
|
{ |
|
|
|
Mappers.Add(new PropertyMapper( |
|
|
|
new PropertyAccessor(sourceClassType, sourceProperty), |
|
|
|
new PropertyAccessor(targetClassType, targetProperty))); |
|
|
|
new PropertyAccessor(sourceProperty), |
|
|
|
new PropertyAccessor(targetProperty))); |
|
|
|
} |
|
|
|
else if (targetType == typeof(string)) |
|
|
|
{ |
|
|
|
Mappers.Add(new StringConversionPropertyMapper( |
|
|
|
new PropertyAccessor(sourceClassType, sourceProperty), |
|
|
|
new PropertyAccessor(targetClassType, targetProperty))); |
|
|
|
new PropertyAccessor(sourceProperty), |
|
|
|
new PropertyAccessor(targetProperty))); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
var converter = TypeDescriptor.GetConverter(targetType); |
|
|
|
|
|
|
|
if (converter.CanConvertFrom(sourceType)) |
|
|
|
{ |
|
|
|
Mappers.Add(new TypeConverterPropertyMapper( |
|
|
|
new PropertyAccessor(sourceProperty), |
|
|
|
new PropertyAccessor(targetProperty), |
|
|
|
converter)); |
|
|
|
} |
|
|
|
else if (sourceType.Implements<IConvertible>() || targetType.Implements<IConvertible>()) |
|
|
|
{ |
|
|
|
Mappers.Add(new ConversionPropertyMapper( |
|
|
|
new PropertyAccessor(sourceClassType, sourceProperty), |
|
|
|
new PropertyAccessor(targetClassType, targetProperty), |
|
|
|
new PropertyAccessor(sourceProperty), |
|
|
|
new PropertyAccessor(targetProperty), |
|
|
|
targetType)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static TTarget MapClass(TSource source, TTarget destination, CultureInfo culture) |
|
|
|
{ |
|
|
|
|