CodeDom - calling a generic method

Does anyone know a way to call a common base class method with CodeDom?

I have no problem calling the standard method, but I cannot find a solution to calling the generic one.

The code I use to call the GetInstance standard base class method is:

CodeAssignStatement assignStatement = new CodeAssignStatement(
     new CodeVariableReferenceExpression("instance"),
     new CodeMethodInvokeExpression(
         new CodeThisReferenceExpression(),
         "GetInstance",
         new CodeExpression[] { new CodeVariableReferenceExpression("instance") }
     ));
+5
source share
1 answer

You can find your answer here in msdn:

scroll down to the C # example (CodeDomGenericsDemo).

The general method is generated:

 public virtual void Print<S, T>()
            where S : new()
        {
            Console.WriteLine(default(T));
            Console.WriteLine(default(S));
        }

and then executed in the following example:

dict.Print<decimal, int>();

Code to generate a method call:

 methodMain.Statements.Add(new CodeExpressionStatement(
                 new CodeMethodInvokeExpression(
                      new CodeMethodReferenceExpression(
                         new CodeVariableReferenceExpression("dict"),
                             "Print",
                                 new CodeTypeReference[] {
                                    new CodeTypeReference("System.Decimal"),
                                       new CodeTypeReference("System.Int32"),}),
                                           new CodeExpression[0])));

( CodeThisReferenceExpression() CodeBaseReferenceExpression() CodeVariableReferenceExpression), , , .

+11

All Articles