Frans Bouma wrote:
> it does come up with a string that works in C# but
> doesn't in VB.NET :)

Well, sometimes.  Sometimes not.  And it also sometimes comes up with
strings that work in VB.NET and sometimes it doesn't...


> Because the types were set in a piece of code that is
> written in C# I thought maybe it was related to that,
> but in a VB.NET app, I too get System.Byte[], so the
> language it is written in is not setting anything
> special, as you described.

Yup.  It's all IL as far as the CLR is concerned.


> > why don't you use the CodeDOM to generate your code?
> 
> I don't know if you've tried to generate code with codedom
> (and I mean a large pile of code) but you're typing your @ss
> off :)

I certainly have.  Looking in the godegen output directory of the last
project I used CodeDOM on, I can see 227K of source code in there, over
77 files.  It involved significant less typing than writing all that
generated code by hand would have - the CodeDom-based generator's source
was just 23K...  (And there are a lot more comments in the generator's
source than in the generated source!)


> Plus, I use templates which are processed by an interpreter,
> CodeDom would be very cumbersome to use in this case; the
> templates together are 330KB of code. Putting all that in
> CodeDom statements is not doable, sorry.

I wasn't suggesting you rearchitect your whole application around
CodeDom.  It is actually possible to use CodeDom for just this one
problem.  Here's a standalone piece of code which you're more than
welcome to drop into your existing application.  It uses the CodeDom
internally, but absolutely does not require your application to embrace
the CodeDom as a whole:


using System;
using System.IO;
using System.CodeDom.Compiler;
using System.CodeDom;
using Microsoft.VisualBasic;

public class CodeUtils
{
    public static string GenVBArrayDeclaration(Type arrayType,
                                               string varName)
    {
        if (arrayType == null)
            throw new ArgumentNullException("arrayType");
        if (varName == null)
            throw new ArgumentNullException("varName");
        if (!arrayType.IsArray)
            throw new ArgumentException("Type must be an array",
                                        "arrayType");

        VBCodeProvider cp = new VBCodeProvider();
        ICodeGenerator cg = cp.CreateGenerator();

        CodeVariableDeclarationStatement stmt =
            new CodeVariableDeclarationStatement(arrayType, varName);
        using (StringWriter output = new StringWriter())
        {
            cg.GenerateCodeFromStatement(stmt, output, null);
            output.Flush();
            return output.ToString();
        }
    }
}

If I run this with the following test code:

  string decl = CodeUtils.GenVBArrayDeclaration(typeof(int[]), "myVar");
  Console.WriteLine(decl);

I get the following output:

  Dim myVar() As Integer

That, surely, must be close to what you want.  And that function only
contains 15 lines of code (ignoring blanks and braces), and 7 of those
are just checking the validity of the parameters!  So the CodeDom
doesn't have to be that complex.  And it doesn't require you to
architect your whole program around it - you won't need to use any more
of the CodeDom than is in this example, because this is completely
self-contained.



-- 
Ian Griffiths
DevelopMentor

===================================
This list is hosted by DevelopMentor�  http://www.develop.com
NEW! ASP.NET courses you may be interested in:

2 Days of ASP.NET, 29 Sept 2003, in Redmond
http://www.develop.com/courses/2daspdotnet

Guerrilla ASP.NET, 13 Oct 2003, in Boston
http://www.develop.com/courses/gaspdotnet

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to