Search code examples
c#fluent

Get the boolean from Func<T, bool>


I want to get the boolean value from the following fluent function:

public IGridWithOptions<T> CursorPointerWhen(Func<T, bool> propertySpecifier)
{
   bool r = ????

   return this;
}

How can this be done ?


Solution

  • You'll need to have a T value in order to call the delegate:

    public IGridWithOptions<T> CursorPointerWhen(Func<T, bool> propertySpecifier)
    {
        T input = GetInputFromSomewhere();
        bool r = propertySpecifier(input);    
        // ...
        return this;
    }
    

    It's impossible to do this without a T. For example, consider this:

    Func<string, bool> longString = x => x.Length > 100;
    

    What is the "value" of that? It only makes any sense in the context of a particular string. We don't have much information about what you're trying to do here, but you'll need to get an instance of T from somewhere - or change your method argument.