//public static bool LocateLoggerMethods(PEFile file, string assemblyName, string className, out Method startLogMethod, out Method endLogMethod) //{ // startLogMethod = endLogMethod = null; // // Check if it is in this assembly itself // if (file.GetThisAssembly().Name() == assemblyName) // { // ClassDef methodLogger = file.GetClass(className); // if (methodLogger != null) // { // return GetLoggerMethodsFromClass(methodLogger, out startLogMethod, out endLogMethod); // } // } // // Check referenced assemblies // foreach (AssemblyRef assemblyRef in file.GetImportedAssemblies()) // { // if (assemblyRef.Name() == assemblyName) // { // ClassRef methodLoggerRef = TryGetMethodLoggerFromAssembly(assemblyRef, className); // if (methodLoggerRef != null) // { // if (GetLoggerMethodsFromClass(methodLoggerRef, out startLogMethod, out endLogMethod)) // return true; // } // } // } // // Not found in this assembly or referenced assemblies. Try loading given assembly and adding it as reference // AssemblyRef newAssemblyRef = file.MakeExternAssembly(assemblyName); // ClassRef newMethodLoggerRef = TryGetMethodLoggerFromAssembly(newAssemblyRef, className); // if (newMethodLoggerRef != null) // { // if (GetLoggerMethodsFromClass(newMethodLoggerRef, out startLogMethod, out endLogMethod)) // return true; // } // return false; //} private static ClassRef TryGetMethodLoggerFromAssembly(PEFile pef, AssemblyRef assemblyRef, string className) { //string fileName = ""; //if (assemblyRef.Name().ToLower().EndsWith(".dll")|| assemblyRef.Name().ToLower().EndsWith(".exe")) // fileName = assemblyRef.Name(); //else // fileName = assemblyRef.Name() + ".exe"; ////Console.WriteLine("TryGetMethodLoggerFromAssembly ->" + fileName); //if (!System.IO.File.Exists(fileName)) //{ // Console.WriteLine(fileName + " not present in current directory. Skipping it in search"); // return null; //} PEFile refFile = pef;//PEFile.ReadPEFile(fileName); ClassDef methodLogger = refFile.GetClass(className); if (methodLogger != null) { ClassRef methodLoggerRef = methodLogger.MakeRefOf(); if (assemblyRef.GetClass(className) == null) { assemblyRef.AddClass(methodLoggerRef); } System.Array.ForEach(methodLogger.GetMethods(), delegate(MethodDef methodDef) { if (methodLoggerRef.GetMethod(methodDef.Name()) == null) { methodLoggerRef.AddMethod(methodDef.Name(), methodDef.GetRetType(), methodDef.GetParTypes()); } }); return(methodLoggerRef); } return(null); }
static void ProcessClass(ClassDef classDef, Method startLogMethod, Method endLogMethod) { // Don't modify the class methods that we are going to emit calls to, otherwise we'll get unbounded recursion. //if (classDef.Name() == methodLoggerClassName) // return; //if (classFilter.PassesFilter(classDef) == false) // return; foreach (NestedClassDef c in classDef.GetNestedClasses()) { ProcessClass(c, startLogMethod, endLogMethod); } Console.WriteLine("Processing Class:" + classDef.Name()); foreach (MethodDef methodDef in classDef.GetMethods()) { if (methodDef.Name().ToLower() == ".ctor" || methodDef.Name().ToLower() == "dispose" || methodDef.Name().ToLower() == "initializecomponent") { continue; } ModifyCode(classDef, methodDef, startLogMethod, endLogMethod); } }
/// <summary> /// Get all the methods of this module with a specified name /// </summary> /// <param name="name">The name of the method(s)</param> /// <returns>An array of all the methods of this module called "name" </returns> public MethodDef[] GetMethods(string name) { //FIXME Contract.Requires(defaultClass != null); return(defaultClass.GetMethods(name)); }