Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
joaofpereira opened this issue Jul 2, 2024 · 0 comments

Comments

@joaofpereira
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant