mono / t4

T4 text templating engine
Other
395 stars 101 forks source link

[Question] TextTransformation compiles but fails when running with a method not found exception? #189

Open joaofpereira opened 4 months ago

joaofpereira commented 4 months ago

Hello all!

I tried to use Mono T4 to create some complex templates, but I found a problem. I created a minimal example that reproduces the problem found, which I present now.

The .csproj is named Example.csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>net48;net7.0</TargetFrameworks>
    <PlatformTargets>x86;x64</PlatformTargets>
  </PropertyGroup>

</Project>

On my simple class I just created a property to get and set it:

namespace Example
{
    public class Helper
    {
        public System.Func<System.Uri, System.Xml.XmlReader> FuncProperty {get;set;}
    }
}

After this, I build the project to get the assembly.

dotnet build /p:Platform=anycpu

The template file I created uses the class from the referenced assembly:

<#
var helper = new Example.Helper
{
    FuncProperty = null
};
#>

When I try to run the template with the command:

t4 Example.tt --out=Example.Generated.cs -P=<absolute-path-prefix>\Example\bin\Debug\net7.0 -r=System.Xml.dll -r=Example.dll --debug --verbose

I got the error saying that my property does not exist.

ERROR: Error running transform: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.MissingMethodException: Method not found: 'Void Example.Helper.set_FuncProperty(System.Func`2<System.Uri,System.Xml.XmlReader>)'.
   at Microsoft.VisualStudio.TextTemplating2534841a.GeneratedTextTransformation.TransformText()
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
   --- End of inner exception stack trace ---
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at Mono.TextTemplating.CompiledTemplate.TemplateProcessor.CreateAndProcess(ITextTemplatingEngineHost host, CompiledAssemblyData templateAssemblyData, String templateAssemblyFile, String fullName, CultureInfo culture, String[] referencedAssemblyFiles)

Can you explain why the T4 tool says the property is not found in the class? It looks like the generated text transformation code compiles but does not run.

mhutch commented 3 days ago

What version are you using? I attempted to repro this on 3.0.0 and it worked fine.