Click here to Skip to main content
15,891,905 members
Articles / Programming Languages / C#

CodeDom Assistant

Rate me:
Please Sign up or sign in to vote.
4.84/5 (26 votes)
21 Sep 20074 min read 139.1K   6.6K   82  
Generating CodeDom Code By Parsing C# or VB
// <file>
//     <copyright see="prj:///doc/copyright.txt"/>
//     <license see="prj:///doc/license.txt"/>
//     <owner name="Daniel Grunwald"/>
//     <version>$Revision: 2522 $</version>
// </file>

using System;
using NUnit.Framework;
using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory.Visitors;
using ICSharpCode.NRefactory.PrettyPrinter;

namespace ICSharpCode.NRefactory.Tests.Output
{
	[TestFixture]
	public class SnippetConversion
	{
		void CS2VB(string input, string expectedOutput)
		{
			SnippetParser parser = new SnippetParser(SupportedLanguage.CSharp);
			INode node = parser.Parse(input);
			// parser.Errors.ErrorOutput contains syntax errors, if any
			Assert.IsNotNull(node);
			Assert.AreEqual("", parser.Errors.ErrorOutput);
			// parser.Specials is the list of comments, preprocessor directives etc.
			PreprocessingDirective.CSharpToVB(parser.Specials);
			// Convert C# constructs to VB.NET:
			node.AcceptVisitor(new CSharpConstructsVisitor(), null);
			node.AcceptVisitor(new ToVBNetConvertVisitor(), null);

			VBNetOutputVisitor output = new VBNetOutputVisitor();
			using (SpecialNodesInserter.Install(parser.Specials, output)) {
				node.AcceptVisitor(output, null);
			}
			// output.Errors.ErrorOutput contains conversion errors/warnings, if any
			// output.Text contains the converted code
			Assert.AreEqual("", output.Errors.ErrorOutput);
			Assert.AreEqual(expectedOutput, output.Text);
		}
		
		void VB2CS(string input, string expectedOutput)
		{
			SnippetParser parser = new SnippetParser(SupportedLanguage.VBNet);
			INode node = parser.Parse(input);
			// parser.Errors.ErrorOutput contains syntax errors, if any
			Assert.IsNotNull(node);
			Assert.AreEqual("", parser.Errors.ErrorOutput);
			// parser.Specials is the list of comments, preprocessor directives etc.
			PreprocessingDirective.VBToCSharp(parser.Specials);
			// Convert VB.NET constructs to C#:
			node.AcceptVisitor(new VBNetConstructsConvertVisitor(), null);
			node.AcceptVisitor(new ToCSharpConvertVisitor(), null);

			CSharpOutputVisitor output = new CSharpOutputVisitor();
			using (SpecialNodesInserter.Install(parser.Specials, output)) {
				node.AcceptVisitor(output, null);
			}
			// output.Errors.ErrorOutput contains conversion errors/warnings, if any
			// output.Text contains the converted code
			Assert.AreEqual("", output.Errors.ErrorOutput);
			Assert.AreEqual(expectedOutput, output.Text);
		}
		
		[Test]
		public void CompilationUnitCS2VB()
		{
			CS2VB(
				@"using System;

public class MyClass
{
   string abc;

   public string Abc { get { return abc; } }

    // This is a test method
    static void M<T>(params T[] args) where T : IDisposable
    {
       Console.WriteLine(""Hello!"");
    }
}",

				@"Imports System

Public Class [MyClass]
	Private m_abc As String

	Public ReadOnly Property Abc() As String
		Get
			Return m_abc
		End Get
	End Property

	' This is a test method
	Private Shared Sub M(Of T As IDisposable)(ParamArray args As T())
		Console.WriteLine(""Hello!"")
	End Sub
End Class
"
			);
		}
		
		
		
		
		[Test]
		public void TypeMembersCS2VB()
		{
			CS2VB(
				"void Test() {}\n" +
				"void Test2() {}",

				@"Private Sub Test()
End Sub
Private Sub Test2()
End Sub
"
			);
		}
		
		[Test]
		public void StatementsCS2VB()
		{
			CS2VB(
				"int a = 3;\n" +
				"a++;",

				@"Dim a As Integer = 3
a += 1
"
			);
		}
		
		
		[Test]
		public void TypeMembersVB2CS()
		{
			VB2CS(
				@"Sub Test()
End Sub
Sub Test2()
End Sub
",
				@"public void Test()
{
}
public void Test2()
{
}
"
			);
		}
		
		[Test]
		public void StatementsVB2CS()
		{
			VB2CS(
				@"Dim a As Integer = 3
a += 1
",
				"int a = 3;\r\n" +
				"a += 1;\r\n"
			);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions