コード例 #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MemberData"/> class for the given <paramref
 /// name="memberInfo"/> with the specified <paramref name="attribute"/> configuration.
 /// </summary>
 /// <param name="memberInfo">The <see cref="MemberData"/> to represent.</param>
 /// <param name="type">The type of the value stored by the member.</param>
 /// <param name="attribute">The <see cref="BinaryContentAttribute"/> configuration.</param>
 internal MemberData(MemberInfo memberInfo, Type type, BinaryContentAttribute attribute)
 {
     MemberInfo = memberInfo;
     Type       = type;
     Attribute  = attribute;
 }
コード例 #2
0
        public object ReadObject(Type type, BinaryContentAttribute attribute = null)
        {
            var att      = attribute ?? new BinaryContentAttribute();
            var encoding = att.Encoding != null?Encoding.GetEncoding(att.Encoding) : null;

            if (type == typeof(string))
            {
                var lenght = att.Length != 0 ? att.Length : Length - att.Offset;
                return(ReadString(encoding, att.StringPattern, lenght, att.Offset));
            }
            else if (type.IsEnumerable())
            {
                throw new InvalidOperationException("Multidimensional arrays cannot be read directly.");
            }
            else if (type == typeof(bool))
            {
                return(ReadBoolean(att.BoolPattern, att.Offset));
            }
            else if (type == typeof(byte))
            {
                return(ReadByte(att.Offset));
            }
            else if (type == typeof(DateTime))
            {
                return(ReadDateTime(att.DateTimePattern, att.Endian, att.Offset));
            }
            else if (type == typeof(decimal))
            {
                return(ReadDecimal(att.Offset));
            }
            else if (type == typeof(double))
            {
                return(ReadDouble(att.Endian, att.Offset));
            }
            else if (type == typeof(short))
            {
                return(ReadInt16(att.Endian, att.Offset));
            }
            else if (type == typeof(int))
            {
                return(ReadInt32(att.Endian, att.Offset));
            }
            else if (type == typeof(long))
            {
                return(ReadInt64(att.Endian, att.Offset));
            }
            else if (type == typeof(sbyte))
            {
                return(ReadSByte(att.Offset));
            }
            else if (type == typeof(float))
            {
                return(ReadSingle(att.Endian, att.Offset));
            }
            else if (type == typeof(ushort))
            {
                return(ReadUInt16(att.Endian, att.Offset));
            }
            else if (type == typeof(uint))
            {
                return(ReadUInt32(att.Endian, att.Offset));
            }
            else if (type == typeof(ulong))
            {
                return(ReadUInt64(att.Endian));
            }
            else if (type.IsEnum)
            {
                return(ReadEnum(type, att.Strict, att.Endian, att.Offset));
            }
            else if (type == typeof(object))
            {
                return(ReadBytes(Length - att.Offset, att.Endian, att.Offset));
            }
            else
            {
                return(ReadCustomObject(type));
            }
        }