コード例 #1
0
        private static void ReadField(Type type, FieldInfo field, object obj, DataStream ds, bool swaped)
        {
            object[] attributes = field.GetCustomAttributes(false);

            FIELD FieldInfo         = null;
            int   Length            = 0;
            bool  ReadCUInt32       = true;
            bool  Swaped            = swaped;
            bool  HaveSwapAttribute = false;

            // Attributes
            foreach (object attribute in attributes)
            {
                if (attribute is FIELD)
                {
                    FieldInfo = attribute as FIELD;
                }
                if (attribute is ARRAY)
                {
                    ARRAY array = attribute as ARRAY;
                    if (array.CUInt32)
                    {
                        ReadCUInt32 = true;
                    }
                    else
                    {
                        ReadCUInt32 = false;
                        Length      = array.GetLength(obj, type);
                    }
                }
                if (attribute is SWAP)
                {
                    Swaped            = ((SWAP)attribute).Swaped;
                    HaveSwapAttribute = true;
                }
                if (attribute is IF)
                {
                    IF If = attribute as IF;
                    if (!If.Check(obj, type))
                    {
                        return;
                    }
                }
                if (attribute is SKIP)
                {
                    ds.Skip(((SKIP)attribute).Length);
                }
            }

            if (FieldInfo == null)
            {
                return;
            }

            // Read array
            if (field.FieldType.IsArray)
            {
                if (ReadCUInt32)
                {
                    Length = (int)ds.ReadCUInt32();
                }
                // Bytes
                if (field.FieldType.Name == "Byte[]")
                {
                    if (!HaveSwapAttribute)
                    {
                        Swaped = false;
                    }

                    field.SetValue(obj, ds.ReadBytes(Length, Swaped));
                    return;
                }

                // Other
                Type  elementType = field.FieldType.GetElementType();
                Array array       = Array.CreateInstance(elementType, Length);

                for (int i = 0; i < array.Length; i++)
                {
                    object data;
                    ds.Read(out data, elementType, FieldInfo.FieldType, Swaped);
                    array.SetValue(data, i);
                }

                field.SetValue(obj, array);
                return;
            }
            // Read value
            else
            {
                object data;
                ds.Read(out data, field.FieldType, FieldInfo.FieldType, Swaped);
                field.SetValue(obj, data);
            }
        }