Skip to content

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

@pabermod

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''

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions