public static bool runPostSharpOnAssembly(string targetAssembly, bool createBackup)
        {
            try
            {
                var tempSourceFile = targetAssembly + ".tmp";
                var fileName       = targetAssembly;
                if (createBackup)
                {
                    O2.DotNetWrappers.Windows.Files.MoveFile(targetAssembly, tempSourceFile);
                    //File.Move(targetAssembly, tempSourceFile);      // we need to do this because the postSharp injection is locking the targetAssembly

                    if (false == (File.Exists(tempSourceFile) && false == File.Exists(targetAssembly)))
                    {
                        DI.log.error("Error in runPostSharpOnAssembly, something went wrong with file move");
                        return(false);
                    }
                    fileName = tempSourceFile;
                }

                var projectConfigurationFile = @"C:\_DinisTest\tools\PostSharp 1.5\Default.psproj";

                PostSharpObjectSettings settings = new PostSharpObjectSettings();
                settings.CreatePrivateAppDomain = true;
                settings.Settings["Trace"]      = "false";

                ApplicationInfo.SetSetting("Trace", "false");

                PropertyCollection properties = new PropertyCollection();

                var tempOutputFile = DI.config.getTempFileInTempDirectory(".exe");
                properties["ResolvedReferences"]    = ".";
                properties["SearchPath"]            = ".";
                properties["Output"]                = targetAssembly; //+".exe"; // @"C:\O2\_tempDir\PS_XTraceTest.exe";
                properties["IntermediateDirectory"] = @"C:\O2\_tempDir";
                properties["CleanIntermediate"]     = "true";
                properties["SignAssembly"]          = "false";
                properties["PrivateKeyLocation"]    = ".";

                ProjectInvocationParameters invocationParameters =
                    new ProjectInvocationParameters(projectConfigurationFile);
                invocationParameters.Properties.Merge(properties);
                ModuleLoadStrategy moduleLoadStrategy = settings.DisableReflection
                                                            ? ((ModuleLoadStrategy)
                                                               new ModuleLoadDirectFromFileStrategy(fileName))
                                                            : ((ModuleLoadStrategy)
                                                               new ModuleLoadReflectionFromFileStrategy(fileName));
                using (IPostSharpObject obj2 = PostSharpObject.CreateInstance(settings, null))
                {
                    obj2.InvokeProject(new ProjectInvocation(invocationParameters, moduleLoadStrategy));
                }
                if (createBackup)
                {
                    File.Delete(tempSourceFile);
                    // if we want to be able to delete this file we need to set settings.CreatePrivateAppDomain = true;
                    if (false == File.Exists(tempSourceFile) && File.Exists(targetAssembly))
                    {
                        return(true);
                    }
                }
                else
                {
                    return(true);
                }
            }
            catch (Exception ex)
            {
                DI.log.info("Error in runPostSharpOnAssembly: {0}", ex.Message);
            }
            return(false);
        }
Esempio n. 2
0
        public static bool runPostSharpOnAssembly(string targetAssembly, bool createBackup)
        {
            try
            {
                var tempSourceFile = targetAssembly + ".tmp";
                var fileName = targetAssembly; 
                if (createBackup)
                {                    
                    O2.DotNetWrappers.Windows.Files.MoveFile(targetAssembly, tempSourceFile);
                    //File.Move(targetAssembly, tempSourceFile);      // we need to do this because the postSharp injection is locking the targetAssembly
                    
                    if (false == (File.Exists(tempSourceFile) && false == File.Exists(targetAssembly)))
                    {
                        DI.log.error("Error in runPostSharpOnAssembly, something went wrong with file move");
                        return false;
                    }
                    fileName = tempSourceFile;
                }

                var projectConfigurationFile = @"C:\_DinisTest\tools\PostSharp 1.5\Default.psproj";

                PostSharpObjectSettings settings = new PostSharpObjectSettings();
                settings.CreatePrivateAppDomain = true;
                settings.Settings["Trace"] = "false";

                ApplicationInfo.SetSetting("Trace", "false");

                PropertyCollection properties = new PropertyCollection();

                var tempOutputFile = DI.config.getTempFileInTempDirectory(".exe");
                properties["ResolvedReferences"] = ".";
                properties["SearchPath"] = ".";
                properties["Output"] = targetAssembly; //+".exe"; // @"C:\O2\_tempDir\PS_XTraceTest.exe";
                properties["IntermediateDirectory"] = @"C:\O2\_tempDir";
                properties["CleanIntermediate"] = "true";
                properties["SignAssembly"] = "false";
                properties["PrivateKeyLocation"] = ".";

                ProjectInvocationParameters invocationParameters =
                    new ProjectInvocationParameters(projectConfigurationFile);
                invocationParameters.Properties.Merge(properties);
                ModuleLoadStrategy moduleLoadStrategy = settings.DisableReflection
                                                            ? ((ModuleLoadStrategy)
                                                               new ModuleLoadDirectFromFileStrategy(fileName))
                                                            : ((ModuleLoadStrategy)
                                                               new ModuleLoadReflectionFromFileStrategy(fileName));
                using (IPostSharpObject obj2 = PostSharpObject.CreateInstance(settings, null))
                {
                    obj2.InvokeProject(new ProjectInvocation(invocationParameters, moduleLoadStrategy));
                }
                if (createBackup)
                {
                    File.Delete(tempSourceFile);
                    // if we want to be able to delete this file we need to set settings.CreatePrivateAppDomain = true;
                    if (false == File.Exists(tempSourceFile) && File.Exists(targetAssembly))
                        return true;
                }
                else                
                    return true;                            
            }
            catch (Exception ex)
            {
                DI.log.info("Error in runPostSharpOnAssembly: {0}", ex.Message);
            }
            return false;
        }