C# CodeVariableDeclarationStatement with array initialization

Hi there, I would like to generate the following using CodeDom

object[] arguments = { Field1, Field2, Field3 };

I have arguments as procedure.Parameters collection. I think it must be like this:

CodeVariableDeclarationStatement cd1 = new CodeVariableDeclarationStatement("object[]", "arguments", parameterListCodeExpression);

I need hellp with correct code for parameterListCodeExpression.

thanks in advance



Answer this question

C# CodeVariableDeclarationStatement with array initialization

  • RussP

    Be careful about CodeArgumentReferenceExpression. This class is used to reference arguments in the current function. If resultSet is not an argument that was passed to you then it won't be referenced by CARE.

    To add direction to any expression (although some won't actually compile) you'll use the CodeDirectionExpression.

    CodeDirectionExpression exprDir = new CodeDirectionExpress(FieldDirection.Out, new CodeVariableReferenceExpression("resultSet"));

    This gives you the expression you want.

    Michael Taylor - 10/9/06


  • Alistair4267

    Thanks a lot, it was very kind of you and really usefull

    I have another issue, could you explain how to pass key word out in argument expression for this example

    ret = SqlCommandGenerator.ExecuteCommandQuery(sqlConn, null, (MethodInfo)MethodBase.GetCurrentMethod(), out resultSet, arguments);

    Actually CodeArgumentReferenceExpression does not accept any additional attributes, but name.

    Look at this code, here is a plenty of different arguments:

    CodeAssignStatement curAssignment = new CodeAssignStatement(

    new CodeVariableReferenceExpression("ret"),

    new CodeMethodInvokeExpression(new CodeTypeReference("SqlCommandGenerator"), "ExecuteCommandQuery",

    new CodeArgumentReferenceExpression[] {

    new CodeArgumentReferenceExpression("sqlConn"), (CodeArgumentReferenceExpression)new CodePrimitiveExpression(null),

    new CodeMethodInvokeExpression(new CodeCastExpression(new CodeTypeReference(MethodInfo), new CodeTypeReference(MethodBase)), "GetCurrentMethod"),

    new CodeVariableReferenceExpression("resultSet"),

    new CodeVariableReferenceExpression("arguments")

    }

    )

    )


  • Sunkyu Hwang

    The CodeDom can be confusing. In this case you are trying to create an array variable and initialize it to an array of objects. However the syntax for creating and initializing arrays depends upon the language. Therefore the CodeVariableDeclarationStatement won't work with them directly. For arrays you must first create the array and then declare the variable.

    // object[] = { Field1, Field2, Field3 };
    CodeExpression[] initializers = {
    new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "Field1"),
    new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "Field2"),
    new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "Field3")
    };
    CodeArrayCreateExpression exprArrCreate = new CodeArrayCreateExpression(typeof(object), initializers);

    // object[] arguments = ...
    CodeVariableDeclarationStatement declArgs = new CodeVariableDeclarationStatement(typeof(object[]), "arguments", exprArrCreate);

    Just for completeness you'll use CodeArrayIndexerExpression to index into an array. Also note that I used types rather than strings as they are the safest.

    Michael Taylor - 9/29/06


  • C# CodeVariableDeclarationStatement with array initialization