<#@ assembly name=" /> <#@ assembly name=" /> <#@ assembly name="/>

Using Enum.GetValues in T4 template not working

242 Views Asked by At

I have to following .tt template :

    <#@ template debug="true" hostspecific="true" language="C#" compilerOptions="/langversion:10" #>
    <#@ assembly name="System" #>
    <#@ assembly name="System.Runtime" #>
    <#@ assembly name="System.Core" #>
    <#@ import namespace="System" #>
    <#@ import namespace="System.Runtime" #>
    <#@ import namespace="System.Linq" #>
    <#@ import namespace="System.Text" #>
    <#@ import namespace="System.Collections.Generic" #>
//...
    <#@ output extension=".cs" #>
    <#
    var myEnum =  System.Enum.GetValues(typeof(MyEnum));
    
    #>

I will get the following error:

Running transformation: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. File name: 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' at Microsoft.VisualStudio.TextTemplating4D7E7108009F697C7036AA3982FCAC502862F98EAAECCE320567876825F6A2D8CB866B97B562037BD24FB0BF58428936B28EDCD4EAEBA43FDAE108D3C1E0E59F.GeneratedTextTransformation.TransformText() at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0) at Microsoft.VisualStudio.TextTemplating.TransformationRunner.PerformTransformation()

which seems to mean the System.Runtime could not be found.

1

There are 1 best solutions below

0
GarethJ On

The below code works for me in Visual Studio 2022 in a Net6 project. What VS version/project type are you using?

<#@ template debug="true" hostspecific="true" language="C#" compilerOptions="/langversion:10" #>
<#@ assembly name="System" #>
<#@ assembly name="System.Runtime" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Runtime" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
<#
    var myEnum =  System.Enum.GetValues(typeof(MyEnum));
    foreach (var member in myEnum)
    {
        this.WriteLine($"// {member}");
    }
    
#>
<#+
    enum MyEnum {
      Red,
      Green,
      Blue
    }
#>