Ejemplo n.º 1
0
        private static IDeepComparer BuildCore(Type t)
        {
            return(Comparers.GetOrAdd(t, type =>
            {
                if (IsSimpleType(type) || IsEquatable(type))
                {
                    return NoopComparer;
                }

                if (IsArray(type))
                {
                    var comparerType = typeof(ArrayComparer <>).MakeGenericType(type.GetElementType() !);

                    return (IDeepComparer)Activator.CreateInstance(comparerType, DefaultComparer) !;
                }

                if (IsSet(type))
                {
                    var comparerType = typeof(SetComparer <>).MakeGenericType(type.GetGenericArguments());

                    return (IDeepComparer)Activator.CreateInstance(comparerType, DefaultComparer) !;
                }

                if (IsDictionary(type))
                {
                    var comparerType = typeof(DictionaryComparer <,>).MakeGenericType(type.GetGenericArguments());

                    return (IDeepComparer)Activator.CreateInstance(comparerType, DefaultComparer) !;
                }

                if (IsCollection(type))
                {
                    PropertyAccessor?count = null;

                    var countProperty = type.GetProperty("Count");

                    if (countProperty != null && countProperty.PropertyType == typeof(int))
                    {
                        count = new PropertyAccessor(type, countProperty);
                    }

                    return (IDeepComparer)Activator.CreateInstance(typeof(CollectionComparer), DefaultComparer, count) !;
                }

                return new ObjectComparer(DefaultComparer, type);
            }));
        }
Ejemplo n.º 2
0
            static ClassCopier()
            {
                var type = typeof(T);

                foreach (var property in type.GetPublicProperties())
                {
                    if (!property.CanWrite || !property.CanRead)
                    {
                        continue;
                    }

                    var accessor = new PropertyAccessor(type, property);

                    if (property.PropertyType.Implements <ICloneable>())
                    {
                        Mappers.Add(new PropertyMapper(accessor, x => ((ICloneable)x)?.Clone()));
                    }
                    else
                    {
                        Mappers.Add(new PropertyMapper(accessor, x => x));
                    }
                }
            }
Ejemplo n.º 3
0
 public PropertyMapper(PropertyAccessor sourceAccessor, PropertyAccessor targetAccessor)
 {
     this.sourceAccessor = sourceAccessor;
     this.targetAccessor = targetAccessor;
 }
Ejemplo n.º 4
0
 public StringConversionPropertyMapper(
     PropertyAccessor sourceAccessor,
     PropertyAccessor targetAccessor)
     : base(sourceAccessor, targetAccessor)
 {
 }