Skip to content

FileNotFoundException (System.Runtime, Version=4.2.1.0) when reflecting in T4 template #2000

@BasPaap

Description

@BasPaap

General

I have a .NET Core 2.1 ASP.NET MVC application in which I have a design time T4 template which I want to use to generate some code. Currently, it looks like this:

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="$(SolutionDir)\Bar\bin\Debug\netcoreapp2.1\Bar.dll" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="Bar" #>
<#@ output extension=".txt" #>
<#
    var myType = typeof(string);
    var myName = y.Name;
    Write(myName);    
#>

This works: the .txt file generated by this template contains "String".
However, now I have a class Foo in Foo.cs in the same folder as my template:

namespace Bar
{
    public class Foo
    {
    }
}

Now I want to use Foo instead of string, so I just change the line
var myType = typeof(string);
to
var myType = typeof(Foo);

and save the template. Now I get an error:

Running transformation: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.

With the following stack trace:

File name: 'System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
    at Microsoft.VisualStudio.TextTemplating9418B77BB4C607966DB4F31F6C9D57D97D9892B08324572567CEAC228ECCD2BE3839F9E2A0AE0ECA2D5DD0CCC161A70762C40A6D67A8DC505F3E88E86A36C7CE.GeneratedTextTransformation.TransformText()
    at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
    at CallSite.Target(Closure , CallSite , Object )   
    at Microsoft.VisualStudio.TextTemplating.TransformationRunner.PerformTransformation()

Why does it throw this error? Why does reflection work for the built-in string type, but somehow depend on .NET Framework 4.2.1 for my own .NET Core 2.1 type?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions