示例#1
0
        private bool GetTypeFields(string ns, string name, out int start, out int end)
        {
            start = end = -1;
            bool            ret     = false;
            FieldIterator   fieldIt = m_ClrFile.Metadata.Tables[(int)CorTokenType.mdtFieldDef] as FieldIterator;
            TypeDefIterator it      = m_ClrFile.Metadata.Tables[(int)CorTokenType.mdtTypeDef] as TypeDefIterator;

            it.MoveToFirst();
            for (; !it.IsEnd; it.MoveToNext())
            {
                string nsName   = GetString(m_ClrFile.Buffer, m_StringStart, it.NameSpace);
                string typeName = GetString(m_ClrFile.Buffer, m_StringStart, it.Name);
                if (0 == typeName.CompareTo(name) && (string.IsNullOrEmpty(ns) || 0 == nsName.CompareTo(ns)))
                {
                    start = (int)it.FieldList;
                    if (it.MoveToNext())
                    {
                        end = (int)it.FieldList;
                    }
                    else
                    {
                        end = (int)fieldIt.RecordNumber;
                    }
                    ret = true;
                    break;
                }
            }
            return(ret);
        }
示例#2
0
        public void ModifyWithField(string fullName, string methodName, uint pos, byte op, string fullCn, string fn)
        {
            string classNs   = string.Empty;
            string className = fullName;
            int    srcIx     = fullName.LastIndexOf('.');

            if (srcIx > 0)
            {
                classNs   = fullName.Substring(0, srcIx);
                className = fullName.Substring(srcIx + 1);
            }
            string ns       = string.Empty;
            string cn       = fullCn;
            int    targetIx = fullCn.LastIndexOf('.');

            if (targetIx > 0)
            {
                ns = fullCn.Substring(0, targetIx);
                cn = fullCn.Substring(targetIx + 1);
            }

            Modify(classNs, className, methodName, (uint offset) => {
                int srcStart, srcEnd;
                bool srcValid = GetTypeFields(ns, cn, out srcStart, out srcEnd);
                if (!srcValid)
                {
                    s_ErrorTxts.Add(string.Format("Can't find src {0}->{1}", cn, srcValid));
                    return;
                }
                FieldIterator it = m_ClrFile.Metadata.Tables[(int)CorTokenType.mdtFieldDef] as FieldIterator;
                for (uint ix = (uint)srcStart; ix < (uint)srcEnd; ++ix)
                {
                    it.MoveTo(ix - 1);
                    string name = GetString(m_ClrFile.Buffer, m_StringStart, it.Name);
                    if (name == fn)
                    {
                        m_ClrFile.Buffer[offset + pos]     = op;
                        m_ClrFile.Buffer[offset + pos + 1] = (byte)(ix & 0xff);
                        m_ClrFile.Buffer[offset + pos + 2] = (byte)((ix >> 8) & 0xff);
                        m_ClrFile.Buffer[offset + pos + 3] = (byte)((ix >> 16) & 0xff);
                        m_ClrFile.Buffer[offset + pos + 4] = (byte)0x04;
                        break;
                    }
                }
            });
        }