views:

613

answers:

1

Hi,

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") }
     ));
+4  A: 

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.

Raymond Roestenburg