コード例 #1
0
        private IEnumerable <MapperPosition> CreateMemberEntries(IBuilderContext context)
        {
            return(Type.GetProperties()
                   .Select(x => new
            {
                Property = x,
                Attribute = x.GetCustomAttributes().OfType <AbstractMemberMapAttribute>().FirstOrDefault(),
                ArrayAttribute = x.GetCustomAttribute <MapArrayAttribute>()
            })
                   .Where(x => x.Attribute != null)
                   .Select(x =>
            {
                var converterBuilder = CreateConverterBuilder(x.ArrayAttribute, x.Attribute);
                if (!converterBuilder.Match(x.Property.PropertyType))
                {
                    throw new ByteMapperException(
                        "Attribute does not match property. " +
                        $"type=[{x.Property.DeclaringType.FullName}], " +
                        $"property=[{x.Property.Name}], " +
                        $"attribute=[{typeof(MapArrayAttribute).FullName}]");
                }

                var builder = new MemberMapperBuilder(converterBuilder)
                {
                    Offset = x.Attribute.Offset,
                    Property = x.Property
                };

                return new MapperPosition(builder.Offset, builder.CalcSize(), builder.CreateMapper(context));
            }));
        }
コード例 #2
0
        private ITypeConfigSyntax <T> ForMemberInternal(string name, int offset, Action <IMemberConfigSyntax> config)
        {
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }

            if (config is null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            var type = typeof(T);
            var pi   = type.GetProperty(name);

            if (pi is null)
            {
                throw new ArgumentException("Name is invalid.", nameof(name));
            }

            var member = new MemberConfigExpression();

            config(member);

            if (member.Expression is null)
            {
                throw new InvalidOperationException("Property is not mapped.");
            }

            var converterBuilder = member.Expression.GetMapConverterBuilder();

            if (!converterBuilder.Match(pi.PropertyType))
            {
                throw new ByteMapperException(
                          "Expression does not match property. " +
                          $"type=[{pi.DeclaringType.FullName}], " +
                          $"property=[{pi.Name}]");
            }

            var builder = new MemberMapperBuilder(converterBuilder)
            {
                Property = pi,
                Offset   = offset
            };

            memberMapBuilders.Add(builder);

            lastOffset = Math.Max(offset, lastOffset) + builder.CalcSize();

            return(this);
        }