Esempio n. 1
0
        public uint Export(object srcObj, bool bHavMag, ref byte[] pOut, uint _nIndex, Type objType)
        {
            if (srcObj == null || pOut == null || pOut.Length < _nIndex + 4)
            {
                return(0);
            }
            Type t = objType;

            string[] sfiarr = UniWebLib.Struct_ST.GetStructMember(t.Name);

            if (sfiarr == null)
            {
                return(0);
            }
            uint nIndex = _nIndex;

            if (bHavMag)
            {
                uint2byte((uint)UNISMAG.UNISTRUCT, ref pOut, nIndex); nIndex += 4;
            }
            uint nCount = (uint)sfiarr.Length - 1;

            for (uint i = 0; i < nCount; i++)
            {
                FieldInfo fino = t.GetField(sfiarr[i]);
                if (fino == null)
                {
                    continue;
                }

                object poValue = fino.GetValue(srcObj);
                Type   itt     = fino.FieldType;
                if (!itt.IsPublic)
                {
                    continue;
                }
                //一般Reserved只放在第一个成员,为加速优化
                if (i == 0)
                {
                    if (itt == typeof(Reserved))
                    {
                        continue;
                    }
                }
                if (itt == typeof(string))
                {
                    if (pOut.Length < nIndex + 4)
                    {
                        return(0);
                    }

                    //对进行Html编码
                    string szValue         = (string)poValue;
                    string szFieldNameTemp = sfiarr[i];
                    //todo
                    if (!string.IsNullOrEmpty(szValue) && (!(szFieldNameTemp == "szPassword" || szFieldNameTemp == "szPasswd" || szFieldNameTemp == "szNewPw" || szFieldNameTemp == "szOldPw" || szFieldNameTemp == "szCurAdminPw")))
                    {
                        szValue = szValue.Replace("'", "‘");
                        //  szValue = szValue.Replace(";", ";");
                        szValue = szValue.Replace(",", ",");
                        szValue = szValue.Replace("?", "?");
                        szValue = szValue.Replace("<", "<");
                        szValue = szValue.Replace(">", ">");
                        szValue = szValue.Replace("(", "(");
                        szValue = szValue.Replace(")", ")");
                        //  szValue = szValue.Replace("@", "@");
                        szValue = szValue.Replace("=", "=");
                        szValue = szValue.Replace("+", "+");
                        szValue = szValue.Replace("*", "*");
                        szValue = szValue.Replace("&", "&");
                        szValue = szValue.Replace("#", "#");
                        szValue = szValue.Replace("%", "%");
                        szValue = szValue.Replace("--", "——");
                        poValue = szValue;
                    }


                    if (poValue != null && (string)poValue != "[NULL]")
                    {
                        UniStruct.UniSZ pValue = new UniStruct.UniSZ((string)poValue);
                        uint2byte((uint)pValue.m_dwType, ref pOut, nIndex); nIndex += 4;
                        if (pOut.Length < nIndex + pValue.Size())
                        {
                            return(0);
                        }
                        Array.Copy(pValue.Get(), 0, pOut, nIndex, pValue.Size());
                        nIndex += pValue.Size();
                    }
                    else
                    {
                        uint2byte((uint)UTFLAG.DEFUNISZTYPE, ref pOut, nIndex); nIndex += 4;
                    }
                }
                else if (itt == typeof(uint) || itt == typeof(uint?) || itt.IsEnum || (itt.IsSerializable && !itt.IsArray))
                {
                    if (pOut.Length < nIndex + 4)
                    {
                        return(0);
                    }
                    //if (poValue != null && ((uint)poValue != uint.MaxValue))
                    if (poValue != null)
                    {
                        UniStruct.UniDW pValue = new UniStruct.UniDW((uint)poValue);
                        uint2byte((uint)pValue.m_dwType, ref pOut, nIndex); nIndex += 4;

                        if (pOut.Length < nIndex + 4)
                        {
                            return(0);
                        }
                        uint2byte((uint)pValue.m_dwValue, ref pOut, nIndex); nIndex += 4;
                    }
                    else
                    {
                        uint2byte((uint)UTFLAG.DEFUNIDWTYPE, ref pOut, nIndex); nIndex += 4;
                    }
                }
                else
                {
                    uint nSize = 0;
                    if (itt.IsArray)
                    {
                        CUniStructArrayCS cacs     = new CUniStructArrayCS();
                        object            poValue2 = fino.GetValue(srcObj);
                        byte[]            bytes    = cacs.Export(poValue2, itt);
                        nSize = (uint)bytes.Length;
                        Array.Copy(bytes, 0, pOut, nIndex + 4, nSize);
                    }
                    else
                    {
                        nSize = Export(poValue, true, ref pOut, nIndex + 4, itt);
                    }
                    uint2byte(nSize | (uint)UTFLAG.UTSTREAM | (uint)UTFLAG.UTVALID | (uint)UTFLAG.UTFILL, ref pOut, nIndex); nIndex += 4;
                    nIndex += nSize;
                }
            }
            return(nIndex - _nIndex);
        }
Esempio n. 2
0
        public uint ExportSize(object srcObj, bool bHavMag, Type objType)
        {
            if (srcObj == null)
            {
                return(0);
            }
            Type t = objType;

            FieldInfo[] fiarr = t.GetFields();
            if (fiarr == null)
            {
                return(0);
            }
            uint nExportSize = 0;

            if (bHavMag)
            {
                nExportSize += 4;
            }
            uint nCount = (uint)fiarr.GetLength(0);

            for (uint i = 0; i < nCount; i++)
            {
                object poValue = fiarr[i].GetValue(srcObj);
                Type   itt     = fiarr[i].FieldType;
                if (!itt.IsPublic)
                {
                    continue;
                }
                //一般Reserved只放在第一个成员,为加速优化
                if (i == 0)
                {
                    if (itt == typeof(Reserved))
                    {
                        continue;
                    }
                }
                nExportSize += 4;
                if (poValue == null)
                {
                    continue;
                }
                if (itt == typeof(string))
                {
                    UniStruct.UniSZ pValue = new UniStruct.UniSZ((string)poValue);
                    nExportSize += pValue.Size();
                }
                else if (itt == typeof(uint) || itt == typeof(uint?) || itt.IsEnum || (itt.IsSerializable && !itt.IsArray))
                {
                    nExportSize += 4;
                }
                else
                {
                    uint nSize = 0;
                    if (itt.IsArray)
                    {
                        CUniStructArrayCS cacs     = new CUniStructArrayCS();
                        object            poValue2 = fiarr[i].GetValue(srcObj);
                        nSize = cacs.ExportSize(poValue2, itt);
                    }
                    else
                    {
                        nSize = ExportSize(poValue, true, itt);
                    }

                    nExportSize += nSize;
                }
            }
            return((uint)nExportSize);
        }