Ejemplo n.º 1
0
        private async Task <string> GetCommandLineForLaunchAsync()
        {
            CommandLineBuilder cb = new CommandLineBuilder();

            cb.AddArguments("/waitfordebugger");

            ///////////////////////////////////////////////////////
            // get the list of assemblies referenced by the project
            var referencedAssemblies = await Properties.ConfiguredProject.Services.AssemblyReferences.GetResolvedReferencesAsync();

            //////////////////////////////////////////////////////////////////////////
            // get the list of other projects referenced by the project being deployed
            var referencedProjects = await Properties.ConfiguredProject.Services.ProjectReferences.GetResolvedReferencesAsync();

            /////////////////////////////////////////////////////////
            // get the target path to reach the PE for the executable

            //... we need to access the target path using reflection (step by step)
            // get type for ConfiguredProject
            var projSystemType = Properties.ConfiguredProject.GetType();

            // get private property MSBuildProject
            var buildProject = projSystemType.GetTypeInfo().GetDeclaredProperty("MSBuildProject");

            // get value of MSBuildProject property from ConfiguredProject object
            // this result is of type Microsoft.Build.Evaluation.Project
            var projectResult = ((System.Threading.Tasks.Task <Microsoft.Build.Evaluation.Project>)buildProject.GetValue(Properties.ConfiguredProject));

            // we want the target path property
            var targetPath = projectResult.Result.Properties.First(p => p.Name == "TargetPath").EvaluatedValue;

            // build a list with the full path for each DLL, referenced DLL and EXE
            List <string> assemblyList = new List <string>();

            foreach (IAssemblyReference reference in referencedAssemblies)
            {
                assemblyList.Add(await reference.GetFullPathAsync());
            }

            // loop through each project that is set to build
            foreach (IBuildDependencyProjectReference project in referencedProjects)
            {
                if (await project.GetReferenceOutputAssemblyAsync())
                {
                    assemblyList.Add(await project.GetFullPathAsync());
                }
            }

            // now add the executable to this list
            assemblyList.Add(targetPath);

            // build a list with the PE files corresponding to each DLL and EXE
            List <string> peCollection = assemblyList.Select(a => a.Replace(".dll", ".pe").Replace(".exe", ".pe")).ToList();

            foreach (string peFile in peCollection)
            {
                cb.AddArguments("/load:" + peFile);
            }

            string commandLine = cb.ToString();

            commandLine = Environment.ExpandEnvironmentVariables(commandLine);

            return(commandLine);
        }