Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeDom - Call a generic method

does anyone know a way to call a generic method of a base class with CodeDom?

I have no problem calling a standard method, but I can't find a solution to call the generic.

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

CodeAssignStatement assignStatement = new CodeAssignStatement(
     new CodeVariableReferenceExpression("instance"),
     new CodeMethodInvokeExpression(
         new CodeThisReferenceExpression(),
         "GetInstance",
         new CodeExpression[] { new CodeVariableReferenceExpression("instance") }
     ));
like image 688
Fionn Avatar asked Dec 30 '08 12:12

Fionn


1 Answers

You can find your answer here in msdn:

scroll down to the C# example (CodeDomGenericsDemo).

A generic method is generated:

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

and later executed in the example:

dict.Print<decimal, int>();

The code to generate the call to the method:

 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])));

(You would use CodeThisReferenceExpression() or CodeBaseReferenceExpression() instead of the CodeVariableReferenceExpression), not sure if that is what you mean by calling the standard base class method.

like image 75
Raymond Roestenburg Avatar answered Sep 24 '22 07:09

Raymond Roestenburg