public static bool InsertHooksAndRunPostSharpOnAssembly(string sourceAssembly, string targetAssembly, string typeToHook, string methodToHook)
        {
            if (false == File.Exists(sourceAssembly))
            {
                DI.log.error("sourceAssembly not found: {0}", sourceAssembly);
                return(false);
            }
            if (PostSharpUtils.containsO2PostSharpHooks(sourceAssembly))
            {
                if (false == BackupRestoreFiles.restore(sourceAssembly))
                {
                    DI.log.error("Aborting PostSharp O2 Hook Insert, since they are already there and there was no backup: {0}", targetAssembly);
                    return(false);
                }
            }
            InjectAttributes.InsertHooks(sourceAssembly, targetAssembly, typeToHook, methodToHook, true);
            if (PostSharpExecution.runPostSharpOnAssembly(targetAssembly))
            {
                return(true);
            }

            // if it fails, restore the source assembly
            BackupRestoreFiles.restore(sourceAssembly);
            return(false);
        }
Esempio n. 2
0
        //public static void addAttributes(string assemblyName, string typeToHook, string methodToHook)
        //{

        public static void InsertHooks(string sourceAssembly, string targetAssembly, string typeToHook, string methodToHook, bool backupFile)
        {
            // if requested back up the current targetAssembly
            if (backupFile && false == BackupRestoreFiles.doesBackupExist(sourceAssembly))
            {
                BackupRestoreFiles.backup(sourceAssembly);
            }
            if (sourceAssembly != targetAssembly)
            {
                File.Copy(sourceAssembly, targetAssembly);
            }

            DI.log.info("Adding an attribute to current Assembly");
            var assembly = CecilUtils.getAssembly(targetAssembly);

            addO2PostSharpHookAttribute(assembly);

            if (typeToHook == "" && methodToHook == "")
            {
                addOnMethodInvocationttribute(assembly, assembly.Name.Name, "*");
            }
            else
            if (typeToHook != "" && methodToHook == "")
            {
                addOnMethodInvocationttribute(assembly, assembly.Name.Name, "*", typeToHook);
            }
            else
            {
                addOnMethodInvocationttribute(assembly, assembly.Name.Name, "*", typeToHook, methodToHook);
            }

            //addOnMethodBoundaryAttribute(assembly, assembly.Name.Name, "*");
            //addOnMethodInvocationttribute(assembly, assembly.Name.Name, "*");

            //var fileName = Path.GetFileName(targetAssembly);
            //var savedAssemby = assemblyName.Replace(fileName, fileName);

            //var savedAssemby = assemblyName;

            // remove publicKey
            assembly.Name.PublicKey = null;

            AssemblyFactory.SaveAssembly(assembly, targetAssembly);

            DI.log.info("\n  _ Assembly saved to: " + targetAssembly + " \n");

            Hacks.patchAssemblyForCecilPostSharpBug(targetAssembly);
        }