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

Can't add custom model to template as a parameter #132

Open
Mike875 opened this issue Aug 9, 2022 · 5 comments
Open

Can't add custom model to template as a parameter #132

Mike875 opened this issue Aug 9, 2022 · 5 comments
Labels

Comments

@Mike875
Copy link

Mike875 commented Aug 9, 2022

I get the exception
The type '' of the parameter '' did not match the type passed to the template.
The types in the template and the type being passed in to the parameter definitely match.
I've added a reference to the correct assembly in the metadata and in the generator.

The custom model contains a collection of a second custom model. The child model then has a collection of another custom model. All other properties in the models are strings and integers.

Any help will be greatly appreciated.

@mhutch
Copy link
Member

mhutch commented Oct 4, 2022

Can you provide a small repro case, or at least a snippet of the code that causes this error?

@mhutch mhutch added the needinfo label Oct 5, 2022
@HGSandhagen
Copy link

I have a similar problem. I set a variable with a type, defined in a custom assembly and get the same error message: "The type 'TestT4.DataItem' of the parameter 'MyVariable' did not match the type passed to the template". The same code works when I use a parameter of type "string". (See commented out part below.)

Program.cs:

using Mono.TextTemplating;
using System.Reflection;
using TestT4;

var location = Assembly.GetEntryAssembly().Location;

string templateContent = "<#@ output extension=\".cs\" #>\r\n<#@ assembly name=\"" + location + "\" #>\r\n<#@ parameter name=\"MyVariable\" type=\"TestT4.DataItem\" #>\r\nConsole.WriteLine(<#=MyVariable.Id#> + \": \" + <#=MyVariable.Name#>);\r\n";
Dictionary<string, object> properties = new Dictionary<string, object> { { "MyVariable", new DataItem() { Id = 1, Name = "Test" } } };

// This works
//string templateContent = "<#@ output extension=\".cs\" #>\r\n<#@ parameter name=\"MyVariable\" type=\"string\" #>\r\nConsole.WriteLine(<#=MyVariable#>);\r\n";
//Dictionary<string, object> properties = new() { { "MyVariable", "Test" } };

Console.WriteLine("TemplateContent");
Console.WriteLine(templateContent);


await CallGenerator(templateContent, properties);

static async Task CallGenerator(string templateContent, Dictionary<string, object> properties) {
    var generator = new TemplateGenerator();

    ParsedTemplate parsed = generator.ParseTemplate("Test1.tt", templateContent);

    TemplateSettings settings = TemplatingEngine.GetSettings(generator, parsed);
    settings.Debug = true;
    settings.Log = Console.Out;

    var session = generator.GetOrCreateSession();
    foreach (var item in properties) {
        session[item.Key] = item.Value;
    }
    
    try {
        (string generatedFilename, string generatedContent) = await generator.ProcessTemplateAsync(parsed, "Test1.tt", templateContent, "Test1.cs", settings);

        if (generator.Errors.HasErrors) {
            Console.WriteLine("Errors:");
            foreach (var item in generator.Errors) {
                Console.WriteLine(item);
            }
        }
        Console.WriteLine("");
        Console.WriteLine(generatedContent);
    }
    catch (Exception ex) {
        Console.WriteLine(ex);
    }
}

DataItem.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestT4 {
    public class DataItem {
        public int Id { get; set; }
        public string? Name { get; set; }
    }
}

@frobert35850
Copy link

I have a similar problem, do you have a solution ?

@HGSandhagen
Copy link

Sorry, no. I solved my problem without T4 template.

@frobert35850
Copy link

Sorry, no. I solved my problem without T4 template.

Finally, I use Razor Engine to templating, it's good

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

No branches or pull requests

4 participants