Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Using a workaround from here here. If you want some more codesamples on Roslyn, look at my project here. In order to use the Visual Basic compilers look here. For more information on Roslyn, take a look at the tag wiki I put together.

Using a workaround from here. If you want some more codesamples on Roslyn, look at my project here. In order to use the Visual Basic compilers look here. For more information on Roslyn, take a look at the tag wiki I put together.

Using a workaround from here. If you want some more codesamples on Roslyn, look at my project here. In order to use the Visual Basic compilers look here. For more information on Roslyn, take a look at the tag wiki I put together.

added 181 characters in body
Source Link
Jeroen Vannevel
  • 11.6k
  • 2
  • 39
  • 79
using System;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.VisualBasic.Syntax;
using Microsoft.CodeAnalysis.VisualBasic;
class Program
{
 static void Main(string[] args)
 {
 var stuffIWantToParse = "Dim foo As String, bar As String";
 var tree = VisualBasicSyntaxTree.ParseText(@"
 Module Module1
 " + stuffIWantToParse +
 @" End Module
 ");
 var compilation = VisualBasicCompilation.Create("MyCompilation", syntaxTrees: new[] { tree });
 var moduleType = compilation.GetTypeByMetadataName("Module1");
 PrintVariableDeclarationByIndex(0, moduleType);
 PrintVariableDeclarationByIndex(1, moduleType);
 Console.Read();
 }
 private static void PrintVariableDeclarationByIndex(int i, INamedTypeSymbol moduleType)
 {
 var variable = moduleType.GetMembers().Skip(i).FirstOrDefault();
 var position = variable.Locations.First().SourceSpan.Start;
 var root = variable.Locations.First().SourceTree.GetRoot();
 var parent = root.FindToken(position).Parent;
 var node = parent.FirstAncestorOrSelf<FieldDeclarationSyntax>();
 var typeInfo = node.Declarators[i].AsClause;
 Console.WriteLine("Modifiers: " + string.Join(",", node.Modifiers));
 Console.WriteLine("Name: " + variable.Name);
 Console.WriteLine("Type: " + typeInfo.Type());
 Console.WriteLine();
 }
}
class Program
{
 static void Main(string[] args)
 {
 var stuffIWantToParse = "Dim foo As String, bar As String";
 var tree = VisualBasicSyntaxTree.ParseText(@"
 Module Module1
 " + stuffIWantToParse +
 @" End Module
 ");
 var compilation = VisualBasicCompilation.Create("MyCompilation", syntaxTrees: new[] { tree });
 var moduleType = compilation.GetTypeByMetadataName("Module1");
 PrintVariableDeclarationByIndex(0, moduleType);
 PrintVariableDeclarationByIndex(1, moduleType);
 Console.Read();
 }
 private static void PrintVariableDeclarationByIndex(int i, INamedTypeSymbol moduleType)
 {
 var variable = moduleType.GetMembers().Skip(i).FirstOrDefault();
 var position = variable.Locations.First().SourceSpan.Start;
 var root = variable.Locations.First().SourceTree.GetRoot();
 var parent = root.FindToken(position).Parent;
 var node = parent.FirstAncestorOrSelf<FieldDeclarationSyntax>();
 var typeInfo = node.Declarators[i].AsClause;
 Console.WriteLine("Modifiers: " + string.Join(",", node.Modifiers));
 Console.WriteLine("Name: " + variable.Name);
 Console.WriteLine("Type: " + typeInfo.Type());
 Console.WriteLine();
 }
}
using System;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.VisualBasic.Syntax;
using Microsoft.CodeAnalysis.VisualBasic;
class Program
{
 static void Main(string[] args)
 {
 var stuffIWantToParse = "Dim foo As String, bar As String";
 var tree = VisualBasicSyntaxTree.ParseText(@"
 Module Module1
 " + stuffIWantToParse +
 @" End Module
 ");
 var compilation = VisualBasicCompilation.Create("MyCompilation", syntaxTrees: new[] { tree });
 var moduleType = compilation.GetTypeByMetadataName("Module1");
 PrintVariableDeclarationByIndex(0, moduleType);
 PrintVariableDeclarationByIndex(1, moduleType);
 Console.Read();
 }
 private static void PrintVariableDeclarationByIndex(int i, INamedTypeSymbol moduleType)
 {
 var variable = moduleType.GetMembers().Skip(i).FirstOrDefault();
 var position = variable.Locations.First().SourceSpan.Start;
 var root = variable.Locations.First().SourceTree.GetRoot();
 var parent = root.FindToken(position).Parent;
 var node = parent.FirstAncestorOrSelf<FieldDeclarationSyntax>();
 var typeInfo = node.Declarators[i].AsClause;
 Console.WriteLine("Modifiers: " + string.Join(",", node.Modifiers));
 Console.WriteLine("Name: " + variable.Name);
 Console.WriteLine("Type: " + typeInfo.Type());
 Console.WriteLine();
 }
}
Source Link
Jeroen Vannevel
  • 11.6k
  • 2
  • 39
  • 79

Finally, a question where we can use Roslyn!

I won't review your own code but instead I whipped up this small showcase of how you might do it with the Roslyn compilers. Do note that it is rather ugly, not in the least because I am not entirely comfortable with it myself yet but also because I know little VB.NET and it appears some things are handled differently from the C# compiler. That being said, this sample implementation can parse both

Dim foo As String

and

Dim foo As String, bar As String 

El codos:

class Program
{
 static void Main(string[] args)
 {
 var stuffIWantToParse = "Dim foo As String, bar As String";
 var tree = VisualBasicSyntaxTree.ParseText(@"
 Module Module1
 " + stuffIWantToParse +
 @" End Module
 ");
 var compilation = VisualBasicCompilation.Create("MyCompilation", syntaxTrees: new[] { tree });
 var moduleType = compilation.GetTypeByMetadataName("Module1");
 PrintVariableDeclarationByIndex(0, moduleType);
 PrintVariableDeclarationByIndex(1, moduleType);
 Console.Read();
 }
 private static void PrintVariableDeclarationByIndex(int i, INamedTypeSymbol moduleType)
 {
 var variable = moduleType.GetMembers().Skip(i).FirstOrDefault();
 var position = variable.Locations.First().SourceSpan.Start;
 var root = variable.Locations.First().SourceTree.GetRoot();
 var parent = root.FindToken(position).Parent;
 var node = parent.FirstAncestorOrSelf<FieldDeclarationSyntax>();
 var typeInfo = node.Declarators[i].AsClause;
 Console.WriteLine("Modifiers: " + string.Join(",", node.Modifiers));
 Console.WriteLine("Name: " + variable.Name);
 Console.WriteLine("Type: " + typeInfo.Type());
 Console.WriteLine();
 }
}

Output:

enter image description here

Using a workaround from here. If you want some more codesamples on Roslyn, look at my project here. In order to use the Visual Basic compilers look here. For more information on Roslyn, take a look at the tag wiki I put together.

lang-cs

AltStyle によって変換されたページ (->オリジナル) /