void PatchMethods() { MethodTable methodTable = (MethodTable)stripped_tables [MethodTable.RId]; if (methodTable == null) { return; } RVA method_rva = RVA.Zero; for (int i = 0; i < methodTable.Rows.Count; i++) { MethodRow methodRow = methodTable[i]; MetadataToken methodToken = MetadataToken.FromMetadataRow(TokenType.Method, i); MethodDefinition method = (MethodDefinition)assembly.MainModule.LookupByToken(methodToken); method_rva = method_rva != RVA.Zero ? method_rva : reflection_writer.CodeWriter.WriteMethodBody(method); methodRow.RVA = method_rva; } }
void ReadMethods() { if (!m_tHeap.HasTable(MethodTable.RId)) { m_meths = new MethodDefinition [0]; return; } MethodTable mTable = m_tableReader.GetMethodTable(); m_meths = new MethodDefinition [mTable.Rows.Count]; for (int i = 0; i < mTable.Rows.Count; i++) { MethodRow mRow = mTable [i]; MethodDefinition meth = new MethodDefinition( m_root.Streams.StringsHeap [mRow.Name], mRow.Flags); meth.RVA = mRow.RVA; meth.ImplAttributes = mRow.ImplFlags; meth.MetadataToken = MetadataToken.FromMetadataRow(TokenType.Method, i); m_meths [i] = meth; } }
void CompleteMethods() { TypeDefTable tdefTable = m_tableReader.GetTypeDefTable(); if (!m_tHeap.HasTable(MethodTable.RId)) { m_meths = new MethodDefinition [0]; return; } MethodTable methTable = m_tableReader.GetMethodTable(); ParamTable paramTable = m_tableReader.GetParamTable(); if (!m_tHeap.HasTable(ParamTable.RId)) { m_parameters = new ParameterDefinition [0]; } else { m_parameters = new ParameterDefinition [paramTable.Rows.Count]; } for (int i = 0; i < m_typeDefs.Length; i++) { TypeDefinition dec = m_typeDefs [i]; int index = i, next; if (index == tdefTable.Rows.Count - 1) { next = methTable.Rows.Count + 1; } else { next = (int)(tdefTable [index + 1]).MethodList; } for (int j = (int)tdefTable [index].MethodList; j < next; j++) { MethodRow methRow = methTable [j - 1]; MethodDefinition mdef = m_meths [j - 1]; if (mdef.IsConstructor) { dec.Constructors.Add(mdef); } else { dec.Methods.Add(mdef); } GenericContext context = new GenericContext(mdef); MethodDefSig msig = m_sigReader.GetMethodDefSig(methRow.Signature); mdef.HasThis = msig.HasThis; mdef.ExplicitThis = msig.ExplicitThis; mdef.CallingConvention = msig.MethCallConv; int prms; if (j == methTable.Rows.Count) { prms = m_parameters.Length + 1; } else { prms = (int)(methTable [j]).ParamList; } ParameterDefinition retparam = null; //TODO: optimize this ParamRow pRow = null; int start = (int)methRow.ParamList - 1; if (paramTable != null && start < prms - 1) { pRow = paramTable [start]; } if (pRow != null && pRow.Sequence == 0) // ret type { retparam = new ParameterDefinition( m_root.Streams.StringsHeap [pRow.Name], 0, pRow.Flags, null); retparam.Method = mdef; m_parameters [start] = retparam; start++; } for (int k = 0; k < msig.ParamCount; k++) { int pointer = start + k; if (paramTable != null && pointer < prms - 1) { pRow = paramTable [pointer]; } Param psig = msig.Parameters [k]; ParameterDefinition pdef; if (pRow != null) { pdef = BuildParameterDefinition( m_root.Streams.StringsHeap [pRow.Name], pRow.Sequence, pRow.Flags, psig, context); pdef.MetadataToken = MetadataToken.FromMetadataRow(TokenType.Param, pointer); m_parameters [pointer] = pdef; } else { pdef = BuildParameterDefinition( string.Concat("A_", mdef.IsStatic ? k : k + 1), k + 1, (ParamAttributes)0, psig, context); } pdef.Method = mdef; mdef.Parameters.Add(pdef); } mdef.ReturnType = GetMethodReturnType(msig, context); MethodReturnType mrt = mdef.ReturnType as MethodReturnType; mrt.Method = mdef; if (retparam != null) { mrt.Parameter = retparam; mrt.Parameter.ParameterType = mrt.ReturnType; } } } uint eprid = CodeReader.GetRid((int)m_reader.Image.CLIHeader.EntryPointToken); if (eprid > 0 && eprid <= m_meths.Length) { m_module.Assembly.EntryPoint = GetMethodDefAt(eprid); } }