Beispiel #1
0
        public MemberMapping(MapperInfo mapperInfo, MemberModel from, MemberModel to, string fromMemberPath)
        {
            _Info = mapperInfo;
            From = from;
            To = to;
            from.GetMember = from.Member.GetGetter();
            to.SetMember = to.Member.GetSetter();

            //FromMemberPath = fromMemberPath;
            Key = From.Type.FullName + "->" + To.Type.FullName;

            if (to.Type != from.Type && !to.Type.IsAssignableFrom(from.Type))
                MemberMapper = CreateMapperMethod.MakeGenericMethod(from.Type,to.Type).Invoke(Mapper.Current,null) as IMapper;

            if (!string.IsNullOrEmpty(fromMemberPath))
            {
                var members = fromMemberPath.Split('.');
                var length = members.Length;
                if (length > 1)
                {
                    MemberPaths = new Getter[length - 1];
                    var subObjectType = from.Type;
                    for (int i = 1; i < length; i++)
                    {
                        var subMember = subObjectType
                            .GetMember(members[i])
                            .FirstOrDefault();
                        if (subMember == null)
                            break;
                        MemberPaths[i - 1] = subMember.GetGetter();
                        subObjectType = subMember.GetMemberType();
                    }
                }
            }
        }
 public EntityMemberMapping(MemberModel memberModel, ValidationAttribute attr)
 {
     Member = memberModel.Member;
     Name = memberModel.Name;
     Getter = memberModel.GetMember;
     ValidationAttribute = attr;
     var displayNameAttr = Member.GetAttribute<DisplayNameAttribute>(false);
     DisplayName = displayNameAttr != null ? displayNameAttr.DisplayName : Member.Name;
 }
Beispiel #3
0
        private IList ReadList(XElement root, MemberModel prop, Type listType)
        {
            var t = TypeHelper.GetElementType(listType);
            var list = ObjectCreator.CreateList(Types.IEnumerableofT.MakeGenericType(t), t, 10);

            var container = GetElementByName(root, XName.Get(prop.Name));
            if (container == null)
                container = root;
            if (container.HasElements)
            {
                var elements = container.Descendants().Where(p => string.Equals(p.Name.LocalName, t.Name, StringComparison.InvariantCultureIgnoreCase)); 
                ReadList(t, elements, list);
            }
            list = Mapper.Map(list,null,listType) as IList;

            object obj = list;
            Read(ref obj, container);
            return obj as IList;
        }
Beispiel #4
0
 private object ReadMember(object x, XElement root, MemberModel prop, Type type, XName name, string value)
 {
     if (type == Types.DBNull)
         return DBNull.Value;
     if (type == Types.DateTime)
         return DateFormat.HasValue()
             ? DateTime.ParseExact(value.ToString(), DateFormat, Culture)
             : DateTime.Parse(value.ToString(), Culture);
     if (type == Types.Decimal)
         return Decimal.Parse(value.ToString(), Culture);
     if (type == Types.TimeSpan)
         return XmlConvert.ToTimeSpan(value.ToString());
     if (type == Types.Boolean)
         return XmlConvert.ToBoolean(value.ToString().ToLower());
     if (type.IsPrimitive)
         return value.ChangeType(type, Culture);
     if (type.IsEnum)
         return Mapper.Map(value, null, type);
     if (type == typeof(string))
         return value;
     if (type == typeof(Guid))
         return string.IsNullOrEmpty(value as string) ? Guid.Empty : new Guid(value);
     if (type == typeof(Uri))
         return new Uri(value, UriKind.RelativeOrAbsolute);
     if (type.IsCollectionTypeExcludeStringAndDictionaryType())
         return ReadList(root, prop, type);
     return ReadNestedElement(root, type, name);
 }
Beispiel #5
0
 private object ReadMember(XElement root, MemberModel prop, Type type, XName name)
 {
     if (type == Types.DBNull)
         return DBNull.Value;
     if (type == Types.ByteArray)
         return ReadByteArray(root);
     if (type.IsCollectionTypeExcludeStringAndDictionaryType())
         return ReadList(root, prop, type);
     return ReadNestedElement(root, type, name);
 }