CodeDom - Call a generic method
Asked Answered
V

1

6

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") }
     ));
Venge answered 30/12, 2008 at 12:53 Comment(0)
J
12

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.

Jardena answered 30/12, 2008 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.