adoconnection / RazorEngineCore

.NET6 Razor Template Engine
MIT License
576 stars 85 forks source link

The type 'List<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Collections, #72

Closed hussein-khamis closed 3 years ago

hussein-khamis commented 3 years ago
        public string GenerateTemplate(XYZ Model, string templateName)
        {
            string templateText = ReadTemplate(templateName);

            IRazorEngineCompiledTemplate<RazorEngineTemplateBase<XYZ>> template = 
                _razorEngine.Compile<RazorEngineTemplateBase<XYZ>> (templateText);

            string result = template.Run(instance =>
            {
                instance.Model = Model;
            });

            return result;

        }

Hello, worked fine with normal models, but with this XYZ model, it is not working and showing me the following error: RazorEngineCore.RazorEngineCompilationException HResult=0x80131500 Source=RazorEngineCore StackTrace: at RazorEngineCore.RazorEngine.CreateAndCompileToStream(String templateSource, RazorEngineCompilationOptions options) at RazorEngineCore.RazorEngine.Compile[T](String content, Action`1 builderAction) at RazorEngineHelper.GenerateTemplate(XYZ Model, String templateName) in C:...\RazorEngineHelper.cs:line 27

Thank you.

adoconnection commented 3 years ago

Hi, can you please post console app example to reproduce this exception?

system.collections is referenced by default so there is something else.

adoconnection commented 3 years ago

@hussein-khamis did you succeed?

hussein-khamis commented 3 years ago

Hello, yes I was able to by adding the assembly reference "System.Collections". Sorry for the late response but thank you!

kemmis commented 2 years ago

In case anyone has this issue, adding the reference this way worked for me:

builder.AddAssemblyReference(Assembly.Load("System.Collections"));