Пример #1
0
 public BaseSerializer(StorageOptions options, TypeInfo typeInfo, int indexColumn) : base(options, typeInfo)
 {
     _indexColumn = typeInfo.GetMemberByIndex(indexColumn, options.MemberType);
     if (_indexColumn == null)
     {
         throw new ArgumentException($"Invalid column index provided to BaseSerializer<{typeof(TKey).Name}, {typeof(TValue).Name}>.");
     }
     else if (_indexColumn.Type.Type != typeof(int) && _indexColumn.Type.Type != typeof(uint))
     {
         throw new ArgumentException($"Column index provided to BaseSerializer<{typeof(TKey).Name}, {typeof(TValue).Name}> is not an integer ({_indexColumn.MemberInfo.Name}).");
     }
 }
Пример #2
0
        public TypeMapper(MemberTypes memberType, IEnumerable <IMemberMetadata> fileMembers)
        {
            Type = new TypeInfo(typeof(T));

            Map = new Dictionary <IMemberMetadata, Member>();

            if (!(memberType == MemberTypes.Field || memberType == MemberTypes.Property))
            {
                throw new ArgumentException(nameof(memberType));
            }

            IEnumerable <Member> typeMembers = Type.Members.Flatten(m => m.Type.Members).Where(m => m.MemberType == memberType);

            if (typeMembers == null)
            {
                return;
            }

            using (var typeMembersEnumerator = typeMembers.GetEnumerator())
            {
                foreach (var fileMemberInfo in fileMembers)
                {
                    if (!typeMembersEnumerator.MoveNext())
                    {
                        return;
                    }

                    var typeMemberType = typeMembersEnumerator.Current.Type.Type;
                    if (typeMemberType.IsArray)
                    {
                        typeMemberType = typeMemberType.GetElementType();
                    }

                    var structureFieldSize = Math.Max(1, typeMembersEnumerator.Current.Cardinality) * UnsafeCache.SizeOf(typeMemberType);
                    var metaFieldSize      = fileMemberInfo.GetElementBitSize() * Math.Max(1, fileMemberInfo.Cardinality) * 8;

                    if (structureFieldSize < metaFieldSize)
                    {
                        return; // TODO Throw: binary size mismatch
                    }
                    Map[fileMemberInfo] = typeMembersEnumerator.Current;
                }

                if (typeMembersEnumerator.MoveNext())
                {
                    return; // TODO: Throw. We have more members declared in code than we need.
                }
            }
        }