public static IEnumerable <string> GetUsingDirectives(this CompilationUnitSyntax tree) { var collector = new UsingCollector(); collector.Visit(tree); return(collector.Usings); }
static void Main(string[] args) { var text = @" namespace RoslynTest { public interface ICalculator { public static int Evaluate(string a,string b); public static string Evaluate2(string a1,string b1); } }"; string path = @"E:\ND.Application\ND.Lib.Application\NDLib\ND.WebService\ND.WebService.Contract\NDFront.WebService.Contract\hotel\IHotelFacility.cs"; text = File.ReadAllText(path); SyntaxTree tree = Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText(text); var root = (Microsoft.CodeAnalysis.CSharp.Syntax.CompilationUnitSyntax)tree.GetRoot(); var firstMember = root.Members[0]; NamespaceDeclarationSyntax NameSpaceDeclaration = (NamespaceDeclarationSyntax)firstMember; InterfaceDeclarationSyntax interfaceDeclaration = (InterfaceDeclarationSyntax)NameSpaceDeclaration.Members[0]; Console.WriteLine("命名空间:" + NameSpaceDeclaration.Name); Console.WriteLine("接口名:" + interfaceDeclaration.Identifier.Value); var collctor = new UsingCollector();//收集非System程序集的引用 collctor.Visit(root); foreach (var item in collctor.Usings) { Console.WriteLine(item.Name.ToString()); } IEnumerable <SyntaxAnnotation> ss = interfaceDeclaration.GetAnnotations("ICalculator"); ss.ToList().ForEach(x => { Console.WriteLine("接口描述:" + x.Data); }); interfaceDeclaration.Members.ToList().ForEach(y => { MethodDeclarationSyntax methodDeclaration = (MethodDeclarationSyntax)y; var paramsDeclaration = methodDeclaration.ParameterList.Parameters; Console.WriteLine("返回类型:" + methodDeclaration.ReturnType + "方法名称:" + methodDeclaration.Identifier); paramsDeclaration.ToList().ForEach(x => { Console.WriteLine("参数类型:" + x.Type + ",参数名称:" + x.Identifier); }); }); Console.ReadKey(); }
private static void Main() { SyntaxTree tree = CSharpSyntaxTree.ParseText(programText); CompilationUnitSyntax root = tree.GetCompilationUnitRoot(); var collector = new UsingCollector(); collector.Visit(root); foreach (var directive in collector.Usings) { Console.WriteLine(directive.Name); } }
public void Start() { SyntaxTree tree = SyntaxTree.ParseCompilationUnit( @"using System; using System.Collections.Generic; using System.Linq; using System.Text; using Roslyn.Compilers; using Roslyn.Compilers.CSharp; namespace TopLevel { using Microsoft; using System.ComponentModel; namespace Child1 { using Microsoft.Win32; using System.Runtime.InteropServices; class Foo { } } namespace Child2 { using System.CodeDom; using Microsoft.CSharp; class Bar { } } }"); var root = (CompilationUnitSyntax)tree.Root; var collector = new UsingCollector(); collector.Visit(root); foreach (var directive in collector.Usings) { Console.WriteLine(directive.Name); } }
public static List <UsingDirectiveSyntax> OnClick(out string code) { code = @"using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; namespace TopLevel { using Microsoft; using System.ComponentModel; namespace Child1 { using Microsoft.Win32; using System.Runtime.InteropServices; class Foo { } } namespace Child2 { using System.CodeDom; using Microsoft.CSharp; class Bar { } } }"; SyntaxTree tree = CSharpSyntaxTree.ParseText(code); var root = (CompilationUnitSyntax)tree.GetRoot(); var collector = new UsingCollector(); collector.Visit(root); return(collector.Usings); }
static void Main(string[] args) { var code = new StreamReader("..\\..\\Class.cs").ReadToEnd(); SyntaxTree tree = CSharpSyntaxTree.ParseText( code); var root = (CompilationUnitSyntax)tree.GetRoot(); var collector = new UsingCollector(); collector.Visit(root); foreach (var directive in collector.Statements) { Console.WriteLine(directive.Kind()); } var methods = root .DescendantNodes() .OfType <MethodDeclarationSyntax>() .Where(n => n.ParameterList.Parameters.Any()) .Where(n => !n.Modifiers.Any(m => m.ToString() == "private")) .ToList(); string testFormat = " [Test]\r\n public void Test{0} ()\r\n {{\r\n var classs = new {1}();\r\n classs.{2}({3});\r\n }}"; int i = 0; foreach (var method in methods) { SyntaxToken className = ((ClassDeclarationSyntax)method.Parent).Identifier; Console.Out.WriteLine(className + "::" + method.Modifiers + " " + method.Identifier + method.ParameterList); Console.Out.WriteLine(string.Format(testFormat, i, className, method.Identifier, "\"test parametro\"")); i++; } Console.Read(); }
// public static void process(UInt64 container) // public static void process(System.UInt64 container) public static void process(string filename) { // Test if this type is allowed. // StringBuilder sss; // StringBuilder sss = filename; csharpBuilder.helloFromCxx(); String filetext = ""; // Console.WriteLine ("In C# process(System.UInt64) called from ROSE C++: Hello!"); Console.WriteLine(filename); using (FileStream fs = File.Open(filename, FileMode.Open)) { byte[] b = new byte[1024]; UTF8Encoding temp = new UTF8Encoding(true); while (fs.Read(b, 0, b.Length) > 0) { // Console.WriteLine ("In C# process(string) reading file! \n"); // Console.WriteLine(temp.GetString(b)); // filetext += b; filetext += temp.GetString(b); } } Console.WriteLine("In C# process(string) after reading file"); // Output the file as text on the console (debugging). // Console.WriteLine (filetext); // SyntaxTree tree = CSharpSyntaxTree.ParseText("using System;"); SyntaxTree tree = CSharpSyntaxTree.ParseText(filetext); Console.WriteLine("In C# process(System.UInt64) after parsing C# file"); var compilation = CSharpCompilation.Create("MyCompilation", syntaxTrees: new[] { tree }); SemanticModel model = compilation.GetSemanticModel(tree); Console.WriteLine("In C# process(System.UInt64) after compiling C# file"); var root = tree.GetRoot() as CSharpSyntaxNode; var builder = new AstBuilder(model); root.Accept(builder); csharpBuilder.basicFinalChecks(); #if IGNORE_NEW_CODE var collector = new UsingCollector(); collector.Visit(root); foreach (var directive in collector.Usings) { Console.WriteLine(directive.Name); } #endif #if VERBOSE_MODE Console.WriteLine("In C# process(System.UInt64) after outputing the using directives!"); Console.WriteLine("In C# process(System.UInt64) generate DOT file of Roslyn AST!"); #endif // VERBOSE_MODE #if OUTPUT_DOT_GRAPH_OF_ABSTRACT_SYNTAX_TREE var buildDotGraph_object = new BuildDotGraph(filename); buildDotGraph_object.Visit(root); // Call the destructor. // buildDotGraph_object.Finalize(); buildDotGraph_object.CloseOffDotSyntax(); #endif #if VERBOSE_MODE Console.WriteLine("DONE: C# process(System.UInt64) generate DOT file of Roslyn AST!"); #endif // VERBOSE_MODE #if OUTPUT_ABSTRACT_SYNTAX_TREE var abstractSyntaxTreeTraversal_object = new AbstractSyntaxTreeTraversal(); abstractSyntaxTreeTraversal_object.Visit(root); #endif #if VERBOSE_MODE Console.WriteLine("In C# process(System.UInt64) output Roslyn AST!"); #endif // VERBOSE_MODE #if OUTPUT_PARSE_TREE var parseTreeTraversal_object = new ParseTreeTraversal(); parseTreeTraversal_object.Visit(root); #endif #if VERBOSE_MODE Console.WriteLine("In C# process(System.UInt64) after output of Roslyn AST!"); Console.WriteLine("Leaving C# process(System.UInt64)"); #endif // VERBOSE_MODE }