Is it possible to get a class and it's properties within a visual studio item template?

76 views Asked by At

I am currently creating a few templates for my team to use within various projects.

I have a very simple "simple mapper" template and a wizard to go along with it that I'd like to expand adding others as I continue.

namespace $rootnamespace$;

public class $safeitemname$
{
    public $mapto$ Map($mapfrom$ entity)
    {
        return new $mapto$()
        {
            Id = entity.Id,
            Name = entity.Name
        };
    }

    public $mapfrom$ MapToDb($mapto$ item)
    {
        return new $mapfrom$()
        {
            Id = item.Id,
            Name = item.Name
        };
    }
}

$mapto$ is replaced with the user's input.

I would like to know if it's possible to look through the hosts solution, get the properties of a class referenced there (e.g. $mapto$) and use this to default the mapping. Is this possible with item templates?

I have seen some reference to T4 templates and will be looking in to those, but would like to know if it's possible with the basic item templates.

1

There are 1 answers

0
Dou Xu-MSFT On

I tested in VS2022 by adding CustomParameter element into vstemplate file for the template and it can get the properties of a class referenced in current project or current solution. After you create an item template, the files for the template are added to a .zip file.The default location is %USERPROFILE%\Documents\Visual Studio \My Exported Templates. Edit the vstemplate file and add custom parameters into the file.

example

<TemplateContent>
    <References />
    <ProjectItem SubType="" TargetFileName="$fileinputname$.cs" ReplaceParameters="true">MapTest.cs</ProjectItem>
<CustomParameters>
    <CustomParameter Name="$mapto$" Value="user’s input : the class you want to reference "/>
    <CustomParameter Name="$mapfrom$" Value=" user’s input : the class you want to reference "/>
</CustomParameters>
  </TemplateContent>