using MonoFN.Cecil; using MonoFN.Cecil.Cil; namespace FishNet.CodeGenerating.Helping.Extension { internal static class MethodDefinitionExtensions { /// /// Clears the method content and returns ret. /// internal static void ClearMethodWithRet(this MethodDefinition md, CodegenSession session, ModuleDefinition importReturnModule = null) { md.Body.Instructions.Clear(); ILProcessor processor = md.Body.GetILProcessor(); processor.Add(session.GetClass().CreateRetDefault(md, importReturnModule)); } /// /// Returns the ParameterDefinition index from end of parameters. /// /// /// /// internal static ParameterDefinition GetEndParameter(this MethodDefinition md, int index) { //Not enough parameters. if (md.Parameters.Count < (index + 1)) return null; return md.Parameters[md.Parameters.Count - (index + 1)]; } /// /// Creates a variable type within the body and returns it's VariableDef. /// internal static VariableDefinition CreateVariable(this MethodDefinition methodDef, TypeReference variableTypeRef) { VariableDefinition variableDef = new VariableDefinition(variableTypeRef); methodDef.Body.Variables.Add(variableDef); return variableDef; } /// /// Creates a variable type within the body and returns it's VariableDef. /// internal static VariableDefinition CreateVariable(this MethodDefinition methodDef, CodegenSession session, System.Type variableType) { return CreateVariable(methodDef, session.GetClass().GetTypeReference(variableType)); } } }