views:

44

answers:

1
CodeVariableDeclarationStatement hashTableParam = new CodeVariableDeclarationStatement();
        hashTableParam.Name = "hastable";
        hashTableParam.Type = new CodeTypeReference(typeof(System.Collections.Hashtable));

here i have created a hashtable data type using code dom . Now i want to use its in-built properties such that add,clear etc to me more clear i want to implement code similar to this one in code dom

ht.add("key","value");

i tried to do like this

CodeVariableDeclarationStatement hashTableParam = new CodeVariableDeclarationStatement();
            hashTableParam.Name = "hastable";
            hashTableParam.Type = new CodeTypeReference(typeof(System.Collections.Hashtable));
            CodeMethodInvokeExpression invokeExp2 =
            new CodeMethodInvokeExpression(new CodeVariableReferenceExpression(hashTableParam.Name), "add");
            invokeExp2.Parameters.Add(new CodeArgumentReferenceExpression("key"));
            invokeExp2.Parameters.Add(new CodeArgumentReferenceExpression("value"));
           // CodeStatementCollection statements = new CodeStatementCollection();
            return hashTableParam;

but i am not able to link between invokeExp2 and hashtableparam .and is there any other solution to use in built properties here i am trying use it has user defined

A: 

The code you've got there looks like you're trying to pass the values of the key and value arguments passed to the method you're building add method of the hashtable:

void GeneratedMethod( string key, string value )
{
    ...
    hashtable.add( key, value );
}

If you're trying to pass the actual words "key" and "value" it looks more like this:

invokeExp2.Parameters.Add( new CodePrimitiveExpression( "key" ) );
invokeExp2.Parameters.Add( new CodePrimitiveExpression( "value" ) );

You've also got it set up to treat the hashTableParam variable itself as a method. Instead you'll want to use a CodeMethodReferenceExpression.

invokeExp2 = 
new CodeMethodInvokeExpression(
    new CodeMethodReferenceExpression( 
        new CodeVariableReferenceExpression( hashTableParam.Name ),
        "add" )        
)
Paul Alexander
i did like that but i want it to be like ht.add("","");the in built property of hash table data type
Arunachalam