Example #1
0
        /// <summary>
        /// Method called at the end of the initialization phase.
        /// </summary>
        internal void Validate()
        {
            if (ExpressionBuilder == null && ManualBuilder == null)
            {
                var targetMembers = TargetType.GetMembers(BindingFlags.Instance | BindingFlags.Public).Where(x => x is FieldInfo || (x is PropertyInfo && (x as PropertyInfo).GetSetMethod() != null)).ToArray();

                var ownMappings = Members.ToDictionary(x => x.Key, x => x.Value);
                Members = new Dictionary <MemberInfo, IMapping>();
                // Get inherit member mappings and before/after map delegates
                foreach (var curMapInfo in _CacheGenericMaps.Where(x => x != this))
                {
                    if (curMapInfo.TargetType.IsAssignableFrom(TargetType) && curMapInfo.SourceType.IsAssignableFrom(SourceType))
                    {
                        if (!_PolymorphTypes.Contains(curMapInfo.SourceType))
                        {
                            _PolymorphTypes.Add(curMapInfo.SourceType);
                        }

                        foreach (var member in curMapInfo.Members)
                        {
                            var memberInfo = targetMembers.Single(x => x.Name == member.Key.Name);
                            AddOrReplaceMapping(memberInfo, member.Value);
                        }
                        var baseBeforeMaps = curMapInfo.BeforeMaps.ToList();
                        baseBeforeMaps.AddRange(BeforeMaps);
                        BeforeMaps = baseBeforeMaps;
                        var baseAfterMaps = curMapInfo.AfterMaps.ToList();
                        baseAfterMaps.AddRange(AfterMaps);
                        AfterMaps = baseAfterMaps;
                    }
                }

                // Own mappings override base class mappings
                foreach (var kpOwnMapping in ownMappings)
                {
                    AddOrReplaceMapping(kpOwnMapping.Key, kpOwnMapping.Value);
                }

                var sourceMembers = SourceType.GetMembers(BindingFlags.Instance | BindingFlags.Public).Where(x => (x is FieldInfo || x is PropertyInfo));

                foreach (var targetMember in targetMembers)
                {
                    var sourceMember = sourceMembers.SingleOrDefault(x => x.Name == targetMember.Name);
                    if (sourceMember != null && !Members.Any(x => x.Key.Name == targetMember.Name))
                    {
                        Members.Add(targetMember, new MemberMapping(sourceMember, RetrievalMode.Default));
                    }
                }
            }
        }