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

Extension Method #550

Open
gertcloete opened this issue Nov 5, 2021 · 3 comments
Open

Extension Method #550

gertcloete opened this issue Nov 5, 2021 · 3 comments
Labels

Comments

@gertcloete
Copy link

gertcloete commented Nov 5, 2021

Hi,

I have written an implementation of IDynamicLinkCustomTypeProvider that load all my custom lynq functions:

When I use my DynamicLinqCustomTypeProvider I can use my Funcs.Left('asdfgh',3) fine as a expression, but unable to use
Funcs.Left('asdfgh',3).Last()

Ie I am trying to make string extension methods that can be chained together. (Example below is just a simple one to show what I am trying to achieve) I have also noticed that GetExtensionMethods on DynamicLinqCustomTypeProvider is never called, but GetCustomTypes is called. Can you please help?

class DynamicLinqCustomTypeProvider : AbstractDynamicLinqCustomTypeProvider, IDynamicLinkCustomTypeProvider
{
public virtual HashSet GetCustomTypes() {...}
public Dictionary<Type, List> GetExtensionMethods() {...}
public Type ResolveType(string typeName) {...}
public Type ResolveTypeBySimpleName(string simpleTypeName) {...}
}

namespace Expressions
{
[DynamicLinqType]
public static class Funcs
{
public static string Left(object text, int numChars)
{
return Left(text as string, numChars);
}
public static string Last(this string input)
{
return input.Substring(str.Length - 1);
}
}
}

To explain the requirement, we are trying to use the dynamic lynq in the following scenario.
Below is the way it work currently, but the bottom example is how I would like to use it. Hence I would like to make extension methods evaluate also. IE I have an custom method: Funcs.Split(...) that return a string[] and then a extension custom method that work in string[] and can join then ie Funcs.TextJoin(...)

In practice we will not Split and TextJoin like the unit test below, as the extension method we want to make is more complicated. The below was just to proof out one can make extension methods, that can be used in the dynamic lynq parser.

[Test]
public async Task Expression_with_textleft_and_last()
{
        dynamic row = new ExpandoObject();
        row.Address = "12 Church Street, Washington,United States";

        string returnValue = _expressionEvalutator.Evaluate<string>(row, "Funcs.TextJoin(\"|\", (Funcs.Split(\",\", Address)))");

        Assert.AreEqual("12 Church Street| Washington|United States", returnValue);
}

[Test]
public async Task Expression_with_textsplit_on_comma_and_join_by_pipe_ext()
{
       dynamic row = new ExpandoObject();
       row.Address = "12 Church Street, Washington,United States";

        string returnValue = _expressionEvalutator.Evaluate<string>(row, "Funcs.Split(\",\", Address).TextJoin(\"|\")");

        Assert.AreEqual("12 Church Street| Washington|United States", returnValue);
}
@StefH StefH added the question label Dec 14, 2021
@StefH
Copy link
Collaborator

StefH commented Sep 10, 2023

Hello @gertcloete, does my answer solve your issue?

1 similar comment
@StefH
Copy link
Collaborator

StefH commented Dec 5, 2023

Hello @gertcloete, does my answer solve your issue?

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

No branches or pull requests

2 participants