示例#1
0
        private async Task <string> RunExecuteAsync(object sender, EventArgs e)
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            var textView = Utils.GetCurrentViewHost(owner)?.TextView;

            if (textView == null)
            {
                return("textView == null");
            }
            SnapshotPoint caretPosition  = textView.Caret.Position.BufferPosition;
            var           roslynDocument = GetRoslynDocument();

            if (roslynDocument == null)
            {
                //owner.ShowMessage("This element is not analyzable in current view.");
                return("roslynDocument == null");
            }
            var ast = await roslynDocument.GetSyntaxRootAsync().ConfigureAwait(false);

            var model = await roslynDocument.GetSemanticModelAsync().ConfigureAwait(false);

            var node = ast.FindNode(new TextSpan(caretPosition.Position, 0), false, true);

            if (node == null)
            {
                //owner.ShowMessage(OLEMSGICON.OLEMSGICON_WARNING, "Can't show ILSpy for this code element!");
                return("node == null");
            }
            bool isstatic = false;
            bool ispublic = false;

            if (node is Microsoft.CodeAnalysis.CSharp.Syntax.MethodDeclarationSyntax)
            {
                Microsoft.CodeAnalysis.CSharp.Syntax.MethodDeclarationSyntax mdy = node as Microsoft.CodeAnalysis.CSharp.Syntax.MethodDeclarationSyntax;

                foreach (var modifier in mdy.Modifiers)
                {
                    Debug.WriteLine(modifier.Text);//public  static
                    if ("public" == modifier.Text.ToLower())
                    {
                        ispublic = true;
                    }
                    if ("static" == modifier.Text.ToLower())
                    {
                        isstatic = true;
                    }
                }
            }
            else
            {
                //owner.ShowMessage(OLEMSGICON.OLEMSGICON_WARNING, "只支持c#,请针对方法!");
                return("只支持c#,请针对方法!");
            }

            if (false == ispublic)
            {
                //owner.ShowMessage(OLEMSGICON.OLEMSGICON_WARNING, "只支持public方法!");
                return("只支持public方法!");
            }


            var symbol = GetSymbolResolvableByILSpy(model, node);

            if (symbol == null)
            {
                //owner.ShowMessage(OLEMSGICON.OLEMSGICON_WARNING, "Can't show ILSpy for this code element!");
                return("symbol == null");
            }

            Debug.WriteLine(symbol.MetadataName);        //                                 Run
            Debug.WriteLine(symbol.ContainingAssembly);  //                           console, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
            Debug.WriteLine(symbol.ContainingModule);    //                              console.dll
            Debug.WriteLine(symbol.ContainingNamespace); //                          fgq.console
            Debug.WriteLine(symbol.ContainingSymbol);    //                              fgq.console.MemoryLeakDemo1
            Debug.WriteLine(symbol.ContainingType);      //                                fgq.console.MemoryLeakDemo1
            Debug.WriteLine(symbol.Name);                //                                          Run



            var roslynProject      = roslynDocument.Project;
            var refsmap            = GetReferences(roslynProject);
            var symbolAssemblyName = symbol.ContainingAssembly?.Identity?.Name;

            // Add our own project as well (not among references)
            var project = FindProject(owner.DTE.Solution.Projects.OfType <EnvDTE.Project>(), roslynProject.FilePath);

            if (project == null)
            {
                //owner.ShowMessage(OLEMSGICON.OLEMSGICON_WARNING, "Can't show ILSpy for this code element!");
                return("project == null");
            }

            string assemblyName      = roslynDocument.Project.AssemblyName;
            string projectOutputPath = Utils.GetProjectOutputAssembly(project, roslynProject);

            //Debug.WriteLine("fgq" + nameof(assemblyName) + ":" + assemblyName);
            //Debug.WriteLine("fgq" + nameof(projectOutputPath) + ":" + projectOutputPath);
            //Debug.WriteLine("fgq project.name" + ":" + project.Name);
            //Debug.WriteLine("fgq project.FileName" + ":" + project.FileName);
            //Debug.WriteLine("fgq project.FullName" + ":" + project.FullName);


            try
            {
                using (ProjectCollection projectCollection = new ProjectCollection())
                {
                    Microsoft.Build.Definition.ProjectOptions projectOptions = new Microsoft.Build.Definition.ProjectOptions();


                    Microsoft.Build.Evaluation.Project buildproject = projectCollection.LoadProject(project.FullName);

                    bool isNetCoreProject = IsNetCoreProject(buildproject);


                    bool buildresult = buildproject.Build();
                    Debug.WriteLine(buildresult);
                    Assembly assembly = Assembly.LoadFile(projectOutputPath);
                    Debug.WriteLine("System.Reflection.Assembly.GetEntryAssembly().FullName:" + Assembly.GetEntryAssembly()?.FullName);
                    Debug.WriteLine("System.Reflection.Assembly.GetExecutingAssembly().FullName:" + Assembly.GetExecutingAssembly()?.FullName);

                    string cmdPath = GetCmdPath(isNetCoreProject);

                    Debug.WriteLine(cmdPath);

                    VSOutput("--------------------------start invoke--------------------------------------------");



                    Type   type = assembly.GetType(symbol.ContainingType.ToString(), false, true);
                    Object o    = assembly.CreateInstance(symbol.ContainingType.Name);

                    ProcessStartInfo processStartInfo = new ProcessStartInfo();
                    processStartInfo.UseShellExecute        = false;;
                    processStartInfo.RedirectStandardError  = true;
                    processStartInfo.RedirectStandardInput  = true;
                    processStartInfo.RedirectStandardOutput = true;
                    processStartInfo.Arguments = string.Format("{0} {1} {2} {3}", projectOutputPath, symbol.ContainingType.ToString(), symbol.Name, isstatic.ToString());

                    processStartInfo.FileName = cmdPath;
                    Process process = Process.Start(processStartInfo);


                    TextReader textReader = process.StandardOutput;


                    process.WaitForExit();

                    //while (false == process.HasExited)
                    //{
                    //}
                    Debug.WriteLine(process.ExitCode);

                    string output = await textReader.ReadToEndAsync();

                    VSOutput(output);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                return(ex.ToString());
            }
            return("");
        }
 public override void VisitMethodDeclaration(Microsoft.CodeAnalysis.CSharp.Syntax.MethodDeclarationSyntax node)
 {
     Check(node.Identifier.ValueText, node.Identifier, GettextCatalog.GetString("Method"));
 }