Exemple #1
0
 public void Add(string key, BinaryLocation value)
 {
     Properties.Add(key, value);
 }
Exemple #2
0
 public void Add(string key, BinaryLocation value)
 {
     Properties.Add(key, value);
 }
        private static OffsetTable CalculateOffsetTable(Type type)
        {
            var offsetTable  = new OffsetTable();
            var flags        = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
            var declaredOnly = type.HasAttribute <DeclaredOnlyAttribute>();

            if (declaredOnly)
            {
                flags |= BindingFlags.DeclaredOnly;
            }
            var properties = type.GetProperties(flags);
            var offset     = 0;

            foreach (var property in properties)
            {
                var attribute = property.GetAttribute <BinaryDataAttribute>(!declaredOnly);
                if (attribute == null)
                {
                    continue;
                }

                if (attribute.Offset != -1)
                {
                    offset = attribute.Offset;
                }
                var location = new BinaryLocation(offset);
                offsetTable.Add(property.Name, location);

                var propertyType = property.PropertyType;
                int length;

                if (attribute.Length.HasValue)
                {
                    if (propertyType.IsArray && BinaryModelBase.BaseType.IsAssignableFrom(propertyType.GetElementType()))
                    {
                        var size = GetOffsetTable(propertyType.GetElementType()).Size;
                        length = attribute.Length.Value * size;
                    }
                    else
                    {
                        length = attribute.Length.Value;
                    }
                }
                else if (BinaryModelBase.BaseType.IsAssignableFrom(propertyType))
                {
                    length = GetOffsetTable(propertyType).Size;
                }
                else if (attribute.StringReadOptions == StringReadOptions.NullTerminated)
                {
                    offsetTable.IsDynamic = true;
                    length = 1;
                }
                else if (propertyType == typeof(DateTime))
                {
                    length = 8;
                }
                else
                {
                    var valueType = propertyType.IsEnum ? Enum.GetUnderlyingType(propertyType) : propertyType;
                    length = valueType.IsValueType ? Marshal.SizeOf(valueType) : 0;
                }
                offset         += length;
                location.Length = length;
            }
            return(offsetTable);
        }