Question

I'm implementing some fairly straight forward code generation using T4, but I'm stuck on a basic problem when it comes to the details of Property generation. When I access the Type property on the CodeProperty objects I want to generate, they are returning 'System.__ComObject' instead of the actual type of the property.

I'm using EnvDTE to find all classes in the project that include my custom attribute. I'm using this to mark certain classes for code generation. So far so good. I'm then stepping over all the CodeElement objects that are the Children of my class. I can find all the properties, it's just I can't get the 'Type' of them.

Here's a snippet of my T4:

public class <#= cls.Name #>_DTO
{
<#
    foreach (CodeElement child in cls.Children)
    {
        if (child.Kind == vsCMElement.vsCMElementProperty)
        {
            var prop = child as CodeProperty;
#>

    public <#= prop.Type.ToString() + " " + child.Name #> { get; set; }

<#
    }
  }
}
#>

And a sample of the output is:

public class TestResult_DTO
{
    public System.__ComObject TestType { get; set; }
}

As you can see, I'm close to valid output, it's just the Type of the property that I'm struggling to access.

Was it helpful?

Solution

Looking at the docs, I suspect you want AsString instead of ToString(). That would call CodeTypeRef.AsString:

AsString return a string representation for the CodeTypeRef in the language being modeled. For example, if the vsCMTypeRef type is vsCMTypeRefInt, then the string would be "Int" for Visual C# and "Long" for Visual Basic.

I've never written this sort of code myself so I'm just going by the documentation, but it's worth a try :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top