コード例 #1
0
ファイル: TypeReflector.cs プロジェクト: Pircs/Fishing-1
    static bool ConvertToBytes(TypeData td, object obj, BinaryWriter bin)
    {
        for (int i = 0; i < td.Fields.Length; ++i)
        {
            TypeData curData = td.Fields[i];
            if (curData.Handle.FieldType.IsPrimitive)
            {
                ITypeReader iReader = TypeReaderMapping.GetReaderFromHash(curData.HashCode);
                object      f       = curData.Handle.GetValue(obj);
                iReader.Write(bin, f, 0);
            }
            else if (curData.Handle.FieldType.IsArray)
            {
                if (curData.ClassType.IsPrimitive)
                {
                    ITypeReader iReader = TypeReaderMapping.GetReaderFromHash(curData.HashCode);
                    object      f       = curData.Handle.GetValue(obj);
                    if (curData.VarType)//写入变长
                    {
                        iReader.Write(bin, f, -1);
                    }
                    else
                    {
                        iReader.Write(bin, f, curData.ArrayCount);
                    }
                }
                else
                {
                    object[] f = (object[])curData.Handle.GetValue(obj);
                    if (curData.VarType)//写入变长
                    {
                        if (f != null && f.Length > 0)
                        {
                            for (int j = 0; j < f.Length; ++j)
                            {
                                ConvertToBytes(curData, f[j], bin);
                            }
                        }
                    }
                    else
                    {
                        for (int j = 0; j < curData.ArrayCount; ++j)
                        {
                            ConvertToBytes(curData, f[j], bin);
                        }
                    }
                }
            }
            else if (curData.HashCode == TypeMapping.STRING_HASH)
            {
                ITypeReader iReader = TypeReaderMapping.GetReaderFromHash(curData.HashCode);
                object      f       = curData.Handle.GetValue(obj);
                if (curData.VarType)
                {
                    iReader.Write(bin, f, -1);
                }
                else
                {
                    iReader.Write(bin, f, curData.ArrayCount);
                }
            }
            else if (curData.Handle.FieldType.IsClass /* || curData.Handle.FieldType.IsValueType*/)
            {
                object f = curData.Handle.GetValue(obj);
                ConvertToBytes(curData, f, bin);
            }
            else
            {
                LogMgr.Log("未知的类型:" + curData.Handle.FieldType.ToString());
                return(false);
            }
        }

        return(true);
    }