Search code examples
c#t4envdte

How to get a function Return Type as a CodeClass2 from a method CodeElement2


Using EnvDTE in a T4 template I can get all methods from a class easily.

Taking it a step further and looping those methods to get the return type as a CodeClass2 (or simply CodeElement2) I get stuck.

((CodeFunction2)codeElement).Type.AsFullName;

This code works fine getting the return type full name, but if I try this;

((CodeFunction2)codeElement).Type as CodeClass2;

I get a null value everytime, even though returning the AsFullName works?

I ultimately need to loop the properties of a functions return type object, but failing miserably at this point.


Solution

  • Looking at the documentation CodeFunction2.Type returns a CodeTypeRef.

    So you should be able to use:

    ((CodeFunction2)codeElement).Type.CodeType as CodeClass2;