Esempio n. 1
0
        internal TypeAdapterSettings GetMergedSettings(Type sourceType, Type destinationType, MapType mapType)
        {
            if (this.RequireExplicitMapping && mapType != MapType.InlineMap)
            {
                if (!this.Dict.ContainsKey(new TypeTuple(sourceType, destinationType)))
                {
                    throw new InvalidOperationException(
                              $"Implicit mapping is not allowed (check GlobalSettings.RequireExplicitMapping) and no configuration exists for the following mapping: TSource: {sourceType} TDestination: {destinationType}");
                }
            }

            var settings = (from rule in this.Rules.Reverse <TypeAdapterRule>()
                            let priority = rule.Priority(sourceType, destinationType, mapType)
                                           where priority != null
                                           orderby priority.Value descending
                                           select rule.Settings).ToList();
            var result = new TypeAdapterSettings
            {
                NoInherit = settings.FirstOrDefault(s => s.NoInherit.HasValue)?.NoInherit
            };

            foreach (var setting in settings)
            {
                result.Apply(setting);
            }
            return(result);
        }
Esempio n. 2
0
        internal TypeAdapterSettings GetMergedSettings(TypeTuple tuple, MapType mapType)
        {
            var arg = new PreCompileArgument
            {
                SourceType      = tuple.Source,
                DestinationType = tuple.Destination,
                MapType         = mapType,
                ExplicitMapping = this.RuleMap.ContainsKey(tuple),
            };
            var settings = (from rule in this.Rules.Reverse <TypeAdapterRule>()
                            let priority = rule.Priority(arg)
                                           where priority != null
                                           orderby priority.Value descending
                                           select rule.Settings).ToList();
            var result = new TypeAdapterSettings();

            foreach (var setting in settings)
            {
                result.Apply(setting);
            }

            //remove recursive include types
            if (mapType == MapType.MapToTarget)
            {
                result.Includes.Remove(tuple);
            }
            else
            {
                result.Includes.RemoveAll(t => t.Source == tuple.Source);
            }
            return(result);
        }
Esempio n. 3
0
        public void Apply(TypeAdapterSettings other)
        {
            if (this.NoInherit == null)
                this.NoInherit = other.NoInherit;

            if (this.NoInherit == true)
            {
                if (this.DestinationType != null && other.DestinationType != null)
                    return;
            }

            if (this.PreserveReference == null)
                this.PreserveReference = other.PreserveReference;
            if (this.ShallowCopyForSameType == null)
                this.ShallowCopyForSameType = other.ShallowCopyForSameType;
            if (this.IgnoreNullValues == null)
                this.IgnoreNullValues = other.IgnoreNullValues;

            this.IgnoreMembers.UnionWith(other.IgnoreMembers);
            this.IgnoreAttributes.UnionWith(other.IgnoreAttributes);
            this.NameMatchingStrategy.Apply(other.NameMatchingStrategy);
            this.DestinationTransforms.TryAdd(other.DestinationTransforms.Transforms);
            this.AfterMappingFactories.AddRange(other.AfterMappingFactories);

            this.ValueAccessingStrategies.AddRange(other.ValueAccessingStrategies);
            this.Resolvers.AddRange(other.Resolvers);

            if (this.ConstructUsingFactory == null)
                this.ConstructUsingFactory = other.ConstructUsingFactory;
            if (this.ConverterFactory == null)
                this.ConverterFactory = other.ConverterFactory;
            if (this.ConverterToTargetFactory == null)
                this.ConverterToTargetFactory = other.ConverterToTargetFactory;
        }
Esempio n. 4
0
        private TypeAdapterSettings CreateSettings(BaseAdaptAttribute attr)
        {
            var settings = new TypeAdapterSettings();
            var setter   = new TypeAdapterSetter(settings, this);

            setter.ApplyAdaptAttribute(attr);
            return(settings);
        }
Esempio n. 5
0
        public TypeAdapterConfig()
        {
            this.Rules = RulesTemplate.ToList();
            var settings = new TypeAdapterSettings();

            this.Default = new TypeAdapterSetter(settings, this);
            this.Rules.Add(new TypeAdapterRule
            {
                Priority = arg => - 100,
                Settings = settings,
            });
        }
Esempio n. 6
0
        public void Apply(TypeAdapterSettings other)
        {
            if (this.NoInherit == null)
            {
                this.NoInherit = other.NoInherit;
            }

            if (this.NoInherit == true)
            {
                if (this.DestinationType != null && other.DestinationType != null)
                {
                    return;
                }
            }

            if (this.PreserveReference == null)
            {
                this.PreserveReference = other.PreserveReference;
            }
            if (this.ShallowCopyForSameType == null)
            {
                this.ShallowCopyForSameType = other.ShallowCopyForSameType;
            }
            if (this.IgnoreNullValues == null)
            {
                this.IgnoreNullValues = other.IgnoreNullValues;
            }

            foreach (var member in other.IgnoreMembers)
            {
                this.MergeIgnoreMembers(member.Key, member.Value);
            }
            this.IgnoreAttributes.UnionWith(other.IgnoreAttributes);
            this.NameMatchingStrategy.Apply(other.NameMatchingStrategy);
            this.DestinationTransforms.TryAdd(other.DestinationTransforms.Transforms);
            this.AfterMappingFactories.AddRange(other.AfterMappingFactories);

            this.ValueAccessingStrategies.AddRange(other.ValueAccessingStrategies);
            this.Resolvers.AddRange(other.Resolvers);

            if (this.ConstructUsingFactory == null)
            {
                this.ConstructUsingFactory = other.ConstructUsingFactory;
            }
            if (this.ConverterFactory == null)
            {
                this.ConverterFactory = other.ConverterFactory;
            }
            if (this.ConverterToTargetFactory == null)
            {
                this.ConverterToTargetFactory = other.ConverterToTargetFactory;
            }
        }
Esempio n. 7
0
        public void Apply(TypeAdapterSettings other)
        {
            if (this.NoInherit == null)
            {
                this.NoInherit = other.NoInherit;
            }

            if (!this.NoInherit.GetValueOrDefault())
            {
                if (this.PreserveReference == null)
                {
                    this.PreserveReference = other.PreserveReference;
                }
                if (this.ShallowCopyForSameType == null)
                {
                    this.ShallowCopyForSameType = other.ShallowCopyForSameType;
                }
                if (this.IgnoreNullValues == null)
                {
                    this.IgnoreNullValues = other.IgnoreNullValues;
                }

                this.IgnoreMembers.UnionWith(other.IgnoreMembers);
                this.IgnoreAttributes.UnionWith(other.IgnoreAttributes);
                this.DestinationTransforms.TryAdd(other.DestinationTransforms.Transforms);

                this.Resolvers.AddRange(other.Resolvers);
            }

            if (this.DestinationType == null ||
                other.DestinationType == null ||
                this.DestinationType.GetTypeInfo().IsAssignableFrom(other.DestinationType.GetTypeInfo()) ||
                other.DestinationType.GetTypeInfo().IsAssignableFrom(this.DestinationType.GetTypeInfo()))
            {
                if (!this.NoInherit.GetValueOrDefault())
                {
                    if (this.ConstructUsing == null)
                    {
                        this.ConstructUsing = other.ConstructUsing;
                    }
                }
                if (this.ConverterFactory == null)
                {
                    this.ConverterFactory = other.ConverterFactory;
                }
                if (this.ConverterToTargetFactory == null)
                {
                    this.ConverterToTargetFactory = other.ConverterToTargetFactory;
                }
            }
        }
Esempio n. 8
0
        public TypeAdapterConfig()
        {
            this.Rules = RulesTemplate.ToList();
            var settings = new TypeAdapterSettings
            {
                ValueAccessingStrategies = ValueAccessingStrategiesTemplate.ToList(),
                NameMatchingStrategy     = NameMatchingStrategy.Exact,
            };

            this.Default = new TypeAdapterSetter(settings, this);
            this.Rules.Add(new TypeAdapterRule
            {
                Priority = (sourceType, destinationType, mapType) => - 100,
                Settings = settings,
            });
        }
Esempio n. 9
0
        internal TypeAdapterSettings GetMergedSettings(TypeTuple tuple, MapType mapType)
        {
            var arg = new PreCompileArgument
            {
                SourceType      = tuple.Source,
                DestinationType = tuple.Destination,
                MapType         = mapType,
                ExplicitMapping = this.RuleMap.ContainsKey(tuple),
            };

            //auto add setting if there is attr setting
            var attrSettings = GetAttributeSettings(tuple, mapType).ToList();

            if (!arg.ExplicitMapping && attrSettings.Any(rule => rule.Priority(arg) == 100))
            {
                GetSettings(tuple);
                arg.ExplicitMapping = true;
            }

            var result = new TypeAdapterSettings();

            lock (this.Rules)
            {
                var rules    = this.Rules.Reverse <TypeAdapterRule>().Concat(attrSettings);
                var settings = from rule in rules
                               let priority = rule.Priority(arg)
                                              where priority != null
                                              orderby priority.Value descending
                                              select rule.Settings;
                foreach (var setting in settings)
                {
                    result.Apply(setting);
                }
            }

            //remove recursive include types
            if (mapType == MapType.MapToTarget)
            {
                result.Includes.Remove(tuple);
            }
            else
            {
                result.Includes.RemoveAll(t => t.Source == tuple.Source);
            }
            return(result);
        }
Esempio n. 10
0
        private TypeAdapterSettings CreateSettings(BaseAdaptAttribute attr)
        {
            var settings = new TypeAdapterSettings();
            var setter   = new TypeAdapterSetter(settings, this);

            if (attr.IgnoreAttributes != null)
            {
                setter.IgnoreAttribute(attr.IgnoreAttributes);
            }
            if (attr.IgnoreNoAttributes != null)
            {
                setter.IgnoreMember((member, _) => !member.GetCustomAttributes(true)
                                    .Select(it => it.GetType())
                                    .Intersect(attr.IgnoreNoAttributes)
                                    .Any());
            }
            if (attr.IgnoreNamespaces != null)
            {
                foreach (var ns in attr.IgnoreNamespaces)
                {
                    setter.IgnoreMember((member, _) => member.Type.Namespace?.StartsWith(ns) == true);
                }
            }
            if (attr.IgnoreNullValues)
            {
                setter.IgnoreNullValues(attr.IgnoreNullValues);
            }
            if (attr.MapToConstructor)
            {
                setter.MapToConstructor(attr.MapToConstructor);
            }
            if (attr.MaxDepth > 0)
            {
                setter.MaxDepth(attr.MaxDepth);
            }
            if (attr.PreserveReference)
            {
                setter.PreserveReference(attr.PreserveReference);
            }
            if (attr.ShallowCopyForSameType)
            {
                setter.ShallowCopyForSameType(attr.ShallowCopyForSameType);
            }
            return(settings);
        }
Esempio n. 11
0
        internal TypeAdapterSettings GetMergedSettings(Type sourceType, Type destinationType, MapType mapType)
        {
            var settings = (from rule in this.Rules.Reverse <TypeAdapterRule>()
                            let priority = rule.Priority(sourceType, destinationType, mapType)
                                           where priority != null
                                           orderby priority.Value descending
                                           select rule.Settings).ToList();
            var result = new TypeAdapterSettings
            {
                NoInherit = settings.FirstOrDefault(s => s.NoInherit.HasValue)?.NoInherit
            };

            foreach (var setting in settings)
            {
                result.Apply(setting);
            }
            return(result);
        }
Esempio n. 12
0
        public void Apply(TypeAdapterSettings other)
        {
            if (this.NoInherit == null)
                this.NoInherit = other.NoInherit;

            if (!this.NoInherit.GetValueOrDefault())
            {
                if (this.PreserveReference == null)
                    this.PreserveReference = other.PreserveReference;
                if (this.ShallowCopyForSameType == null)
                    this.ShallowCopyForSameType = other.ShallowCopyForSameType;
                if (this.IgnoreNullValues == null)
                    this.IgnoreNullValues = other.IgnoreNullValues;

                this.IgnoreMembers.UnionWith(other.IgnoreMembers);
                this.IgnoreAttributes.UnionWith(other.IgnoreAttributes);
                this.DestinationTransforms.TryAdd(other.DestinationTransforms.Transforms);

                this.Resolvers.AddRange(other.Resolvers);
            }

            if (this.DestinationType == null 
                || other.DestinationType == null 
                || this.DestinationType.GetTypeInfo().IsAssignableFrom(other.DestinationType.GetTypeInfo()) 
                || other.DestinationType.GetTypeInfo().IsAssignableFrom(this.DestinationType.GetTypeInfo()))
            {
                if (!this.NoInherit.GetValueOrDefault())
                {
                    if (this.ConstructUsing == null)
                        this.ConstructUsing = other.ConstructUsing;
                }
                if (this.ConverterFactory == null)
                    this.ConverterFactory = other.ConverterFactory;
                if (this.ConverterToTargetFactory == null)
                    this.ConverterToTargetFactory = other.ConverterToTargetFactory;
            }
        }
Esempio n. 13
0
 public TypeAdapterSetter(TypeAdapterSettings settings, TypeAdapterConfig config)
 {
     this.Settings = settings;
     this.Config   = config;
 }
Esempio n. 14
0
 public TypeAdapterSetter(TypeAdapterSettings settings, TypeAdapterConfig parentConfig)
 {
     this.Settings     = settings;
     this.ParentConfig = parentConfig;
 }