Skip to content

Custom extension not working when using IEnumerable<T> #982

Open

Description

@pabermod

I have the following extensions:

public static class GenericExtensions
    {
        public static bool IsEmpty<T>(this T source)
        {
            return source == null;
        }

        public static bool HasOneGeneric<T>(this IEnumerable<T> source)
        {
            return source.Count() == 1;
        }

        public static bool HasOne(this IEnumerable source)
        {
            return true; // FAKE CODE
        }
    }

I have this classes:

public class Person 
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public bool HasName()
        {
            return !string.IsNullOrEmpty(Name);
        }
    }   

    public class House
    {
        public string Name { get; set; }
        public List<Person> Persons { get; set; }
    }

And my code:

 IEnumerable<Person> data = new List<Person>
 {
     new Person { Name = "Alice", Age = 30 },
     new Person { Name = "Bob", Age = 25 },
     new Person { Name = "Charlie", Age = 35 }
 };

 IEnumerable<House> houses = new List<House>
 {
     new House { Persons = new List<Person> { new Person { Name = "Alice", Age = 30 }, new Person { Name = "Bob", Age = 25 }, new Person { Name = "Charlie", Age = 35 } } },
     new House { Persons = new List<Person> { new Person { Name = "Dave", Age = 40 }, new Person { Name = "Eve", Age = 28 } } }
 };

 // Configure Dynamic LINQ to use our custom type provider
 var config = new ParsingConfig
 {
     CustomTypeProvider = new MyCustomTypeProvider(),
 
 };

 string filter = "a => a.Name.IsEmpty()";

 System.Linq.Expressions.Expression<Func<House, bool>> expression = System.Linq.Dynamic.Core.DynamicExpressionParser.ParseLambda<House, bool>(config, false, filter);
 Func<House, bool> function = expression.Compile();

The filter "a => a.Name.IsEmpty()"; does not compile with error System.Linq.Dynamic.Core.Exceptions.ParseException: 'No applicable method 'IsEmpty' exists in type 'String''

The filter "a => GenericExtensions.IsEmpty(a.Name)"; compiles OK.

The filter "a => GenericExtensions.HasOne(a.Persons)"; compiles OK

The filter "a => GenericExtensions.HasOneGeneric(a.Persons)"; does not compile with error System.Linq.Dynamic.Core.Exceptions.ParseException: 'No applicable method 'HasOneGeneric' exists in type 'GenericExtensions''

Activity

  1. menes97 commented on Nov 19, 2026

    @menes97
    Contributor

    Reproduced on current master (0297001, v1.8.0, net10.0) with a DefaultDynamicLinqCustomTypeProvider subclass that adds the extension class to GetCustomTypes():

    • a => GenericExtensions.IsEmpty(a.Name) and a => GenericExtensions.HasOne(a.Persons) work.
    • a => GenericExtensions.HasOneGeneric(a.Persons) fails with No applicable method 'HasOneGeneric' exists in type 'GenericExtensions'.
    • a => a.Persons.HasOne() and a => a.Name.IsEmpty() fail with InvalidOperationException: No generic method '...' on type 'System.Linq.Enumerable' is compatible.... On 1.8.0 I get that exception rather than the ParseException in your report.
    • Even with a non-enumerable receiver, a => a.Persons.Count.IsEmpty() fails with a ParseException, so this T extensions don't resolve as instance calls at all.

    What I found in the code:

    1. DefaultDynamicLinqCustomTypeProvider (around line 124) keys extension methods by the declared type of their first parameter. For this T or this IEnumerable<T> that is an open generic type, so MethodFinder (around line 139) never matches it against a concrete type like string or List<Person>.
    2. For the static call, ExpressionPromoter.Promote (around line 37) only accepts a bare generic parameter (type.IsGenericParameter), not IEnumerable<T>. The MakeGenericMethod step in ExpressionParser (around line 2017) also only infers T for parameters typed exactly T.
    3. For any receiver that implements IEnumerable<> (including string), ParseMemberAccess goes to TryParseEnumerable first (around lines 1987–1993). That builds Expression.Call(typeof(Enumerable), name, ...) and never looks at custom extension methods.

    Workaround for now: call the extension statically with a non-generic or concrete signature, e.g. GenericExtensions.HasOne(a.Persons).

    I haven't tried a fix yet. Would you take a PR for generic extension support (registering by generic definition plus type inference)?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions