在代码dom中如何使用内置属性

时间:2022-02-13 23:41:16
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.

这里我使用代码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 .

现在我想使用它的内置属性,以便添加,清除等等。更清楚我想在代码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 .

但我无法在invokeExp2和hashtableparam之间建立链接。

Is there any other solution to use in built properties here i am trying use it has user defined

是否有任何其他解决方案在建筑属性中使用我在尝试使用它具有用户定义

1 个解决方案

#1


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:

如果你试图传递实际的单词“key”和“value”,它看起来更像是这样的:

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.

您还将其设置为将hashTableParam变量本身视为一种方法。相反,您将需要使用CodeMethodReferenceExpression。

invokeExp2 = 
new CodeMethodInvokeExpression(
    new CodeMethodReferenceExpression( 
        new CodeVariableReferenceExpression( hashTableParam.Name ),
        "add" )        
)

#1


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:

如果你试图传递实际的单词“key”和“value”,它看起来更像是这样的:

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.

您还将其设置为将hashTableParam变量本身视为一种方法。相反,您将需要使用CodeMethodReferenceExpression。

invokeExp2 = 
new CodeMethodInvokeExpression(
    new CodeMethodReferenceExpression( 
        new CodeVariableReferenceExpression( hashTableParam.Name ),
        "add" )        
)