Results 1 to 3 of 3

Thread: automated C# to vb.net translation

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    automated C# to vb.net translation

    .Net has built in compiling and code generation via the CodeDom namespace. The code generater takes a CodeDom object (a language inspecific program) and writes sourcecode for it in either vb or c# depending on which object set you use.

    I'm interested in translating some c# code to vb using the codedom.

    The c# to vb translator I wrote uses this code to translate:

    VB Code:
    1. Dim cs As New Microsoft.CSharp.CSharpCodeProvider
    2.         Dim csParser As CodeDom.Compiler.ICodeParser = cs.CreateParser
    3.         [B]Dim DOM As CodeDom.CodeCompileUnit = csParser.Parse(New IO.StringReader(tC.Text))[/B]
    4.  
    5.         Dim vb As New Microsoft.VisualBasic.VBCodeProvider
    6.         Dim vbGenerator As CodeDom.Compiler.ICodeGenerator = vb.CreateGenerator()
    7.         Dim Output As System.Text.StringBuilder()
    8.         Dim Dest As New IO.StringWriter(Output)
    9.         vbGenerator.GenerateCodeFromCompileUnit(DOM, Dest, New CodeDom.Compiler.CodeGeneratorOptions)
    10.         tV.Text = Output.ToString

    The bolded line is where execution stops and I get a Null Reference Exception. What gives?

    The source of the C# code is a textbox names 'tc' and the vb output should go to 'tv'.

    Let me know if this can be done... it should have no problems.

  2. #2
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: automated C# to vb.net translation

    An article about that in DevX:
    Generate .NET Code in Any Language Using CodeDOM
    In that example, they used GenerateCodeFromNamespace and GenerateCodeFromType, but you can also use: GenerateCodeFromExpression, GenerateCodeFromCompileUnit, GenerateCodeFromStatement.
    There is an example to download also.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Re: automated C# to vb.net translation

    Quote Originally Posted by jcis
    An article about that in DevX:
    Generate .NET Code in Any Language Using CodeDOM
    In that example, they used GenerateCodeFromNamespace and GenerateCodeFromType, but you can also use: GenerateCodeFromExpression, GenerateCodeFromCompileUnit, GenerateCodeFromStatement.
    There is an example to download also.
    I'm most interested in GenerateCodeFromCompileUnit, but the problem I'm having is CreateParser is returning a null reference instead of a parser which can provide a CodeCompileUnit that can be outputted to code.

    Is there any other way to get a CodeCompileUnit from C# or VB.Net code or from a compiled assembly?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width