0

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.

Jiale Xue - MSFT
  • 3,560
  • 1
  • 6
  • 21
G Rushton
  • 1
  • 1
  • Have you tried to add `CustomParameter` element in your vstemplate file for the template? You can enter the value you want to replace. And it works fine on my side. – Dou Xu-MSFT Feb 23 '23 at 01:38

1 Answers1

0

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>
Dou Xu-MSFT
  • 880
  • 1
  • 1
  • 4