Exemplo n.º 1
0
        //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);
        }