RazorLight icon indicating copy to clipboard operation
RazorLight copied to clipboard

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : 'string' does not contain a definition for 'Pluralize'

Open wendellmva opened this issue 6 years ago • 4 comments

Describe the bug Extension method not working as expected.

To Reproduce

[Fact]
public async Task Should_Compile_Template()
{
	var engine = new RazorLightEngineBuilder()
		.UseMemoryCachingProvider()
		.Build();

	var template = "@using Humanizer@(Model.Name.Pluralize())";
	var component = new Component { Name = "Customer"};
	var result = await engine.CompileRenderAsync("templateKey", template, component);
	Assert.That(result, Is.EqualTo(component.Name.Pluralize()));
}

Expected behavior Should resolve to 'Customers' but it does not

Information (please complete the following information):

  • OS: [ Windows 10 pro ]
  • Platform [.NET Core 2.1]
  • RazorLight version [ 2.0-beta1 ]

Additional context However explicit use of an extensions method passes as expected:

[Fact]
public async Task Should_Compile_Template()
{
    var engine = new RazorLightEngineBuilder()
		.UseMemoryCachingProvider()
		.Build();

	var template = "@using Humanizer@(InflectorExtensions.Pluralize(Model.Name))";
	var component = new Component { Name = "Customer" };
	var result = await engine.CompileRenderAsync("templateKey", template, component);
	Assert.That(result, Is.EqualTo(InflectorExtensions.Pluralize(component.Name)));
}

wendellmva avatar Sep 19 '18 13:09 wendellmva

I have wrapped an extension method around the humanizer Pluralize() which was defined in the executing assembly, even that does not work, so its definitely extension methods;

wendellmva avatar Sep 19 '18 13:09 wendellmva

i'm also facing the same issue.

fleischman718 avatar Apr 09 '19 15:04 fleischman718

@wendellmva Have you tried something like this yet?

var engine = new RazorLightEngineBuilder()
                .AddDefaultNamespaces("Humanizer")
		.UseMemoryCachingProvider()
		.Build();

EDIT: Make sure you also have a model to your Component class, otherwize you might have some issues:

var template = "@model Your.Namespace.Component" + Environment.NewLine +
               "Model.Name.Pluralize()";

bleuthoot-sven avatar May 06 '19 09:05 bleuthoot-sven

Marked as a docs issue based on @bleutwo feedback. Thank you, Sven. I will look at the regression suite to see if this behavior is defined.

jzabroski avatar Dec 18 '19 16:12 jzabroski