This sample shows how to programmatically convert C# code to VB.NET and viceversa. I am using the sharpdevelop's ICSharpCode.NRefactory namespaces here to convert code. You can download the assemblies from http://www.icsharpcode.net/OpenSource/SD/Download/
The first parameter of the method is the actual code that need to be converted. Second parameter specifies whether to convert from C# to VB.NET or viceversa. Pass 'true' to convert from C# to VB.NET. Pass 'false' to convert from VB.NET to C#.
Private Function ConvertCodeSnippet(ByVal codeToConvert as string, ByVal csharpToVb As Boolean) as string Dim input As New StringReader(codeToConvert) Dim parser As IParser
parser = ParserFactory.CreateParser(IIf(csharpToVb, SupportedLanguage.CSharp, SupportedLanguage.VBNet), input)
parser.Parse()
If (parser.Errors.count > 0) Then return parser.Errors.ErrorOutput End If
Dim cu As CompilationUnit, output As IOutputASTVisitor cu = parser.CompilationUnit
output = IIf(csharpToVb, CType(New VBNetOutputVisitor(), IOutputASTVisitor), CType(New CSharpOutputVisitor(), IOutputASTVisitor))
cu.AcceptVisitor(output, DBNull.Value) return output.Text End Function
|
No responses found. Be the first to respond and make money from revenue sharing program.
|