Exemplo n.º 1
0
        public EntityMapper GetEntityMapper(Type type, bool build = true)
        {
            EntityMapper mapper = GetEntityMapper(type, build, IncludeFields, IncludeNonPublicProperties, false);

            return(mapper);
        }
Exemplo n.º 2
0
        protected EntityMapper BuildEntityMapper(Type type, bool includeFields = false, bool includeNonPublic = false, bool ignoreSetter = false)
        {
            var mapper = new EntityMapper {
                Members = new List <MemberMapper>(),
                ForType = type
            };

            MethodInfo[] listMethods = type.GetMethods();
            mapper.Serializer   = Reflection.MethodWithAttribute <Serialize>(listMethods);
            mapper.Deserializer = Reflection.MethodWithAttribute <Deserialize>(listMethods);

            if (mapper.Serializer != null && mapper.Deserializer != null)
            {
                return(mapper);
            }

            IEnumerable <MemberInfoWithMeta> members = null;
            bool isIndexed = IsTypeIndexed(type);

            if (isIndexed)
            {
                members = GetIndexedTypeMembers(type);
            }
            else
            {
                members = GetTypeMembers(type, includeFields, includeNonPublic);
            }

            foreach (MemberInfoWithMeta memberWithMeta in members)
            {
                MemberInfo    memberInfo = memberWithMeta.Info;
                string        name       = memberInfo.Name;
                GenericGetter getter     = null;
                GenericSetter setter     = null;

                try {
                    getter = Reflection.CreateGenericGetter(type, memberInfo);
                    setter = Reflection.CreateGenericSetter(type, memberInfo);
                }
                catch (Exception ex) {
                    throw new Exception("Could not generate getter and setter for type: " + type.ToString() + " member: " + name);
                }

                if (ignoreSetter == false)
                {
                    if (getter == null || setter == null)
                    {
                        continue; //They're null when they don't exist
                    }
                }
                Type dataType = memberInfo is PropertyInfo ?
                                (memberInfo as PropertyInfo).PropertyType :
                                (memberInfo as FieldInfo).FieldType;

                var member = new MemberMapper {
                    Name       = name,
                    DataType   = dataType,
                    Getter     = getter,
                    Setter     = setter,
                    SkipIsNull = memberWithMeta.SkipIsNull,
                    SkipType   = memberWithMeta.SkipType
                };
                mapper.Members.Add(member);
            }

            return(mapper);
        }