Skip to content

CLR extension methods #1064

@KoalaBear84

Description

@KoalaBear84

Saw a question on Stack Overflow while researching and couldn't get it to work with the extension methods provided by C# itself.

// Works
public class Foo
{
    public string Name { get; } = "Bar";
}

public static class CustomStringExtensions
{
    public static bool StartsWith(this string value, string value2) => value.StartsWith(value2);
}

Engine engine = new Engine(options =>
{
    options.AddExtensionMethods(typeof(CustomStringExtensions));
});

bool result = engine
    .SetValue("foo", new Foo())
    .Evaluate("foo.Name.StartsWith('B')")
    .AsBoolean();
// Doesn't work
public class Foo
{
    public string Name { get; } = "Bar";
}

Engine engine = new Engine(options =>
{
	// Also tried these
	//options.AllowClr();
	//options.AllowOperatorOverloading();
	//options.AddExtensionMethods(typeof(String));
	options.AddExtensionMethods(typeof(string));
});

bool result = engine
	.SetValue("foo", new Foo())
	.Evaluate("foo.Name.StartsWith('B')")
	.AsBoolean();

bool result = engine
    .SetValue("foo", new Foo())
    .Evaluate("foo.Name.StartsWith('B')")
    .AsBoolean();

Is it because some overloading? I'm curious about the reason.

I see there isn't really anything documented about how the extension method support is to be used. Could an example be added, and maybe also that native extensions methods do not work, or if they do, how they can be added. Thanks!

When will version 3 become available, as I see it's already beta since Oct 2016? :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions