2
votes

I've been struggling with a delegate problem. I am sending a method as a parameter, which don't get parameter and returns generic type Result<T>.

public Result<Content> GetContents()        
    {         
        var contents = new Result<List<TypeLibrary.Content>>();
        contents.Object = context.GetContents();
        if (contents.Object != null)
        {
                contents.Success = true;
            }
            else
            {
                contents.Success = false;
            }            
            return contents;
    }

public static Result<T> Invoker<T>(Func<Result<T>> genericFunctionName) where T : new()
    {
        Result<T> result;
        try
        {
            //do staff               
            result = genericFunctionName();                           
        }
        catch (Exception)
        {
            throw;
        }
        return result;
    }

model is

public class Result<T> where T : new()
{
    public bool Success { get; set; }
    public string Message { get; set; }        
    public T Object { get; set; }

    public Result()
    {
        Object = new T();
    }
}

usage is

var result = InvokerHelper.Invoker(_content.GetContents());

but I get

The type arguments for method InvokerHelper.Invoker<T>(System.Func<TypeLibrary.Base.Result<T>>) cannot be inferred from the usage. Try specifying the type arguments explicitly.

while compiling.

What is wrong with that?

1

1 Answers

6
votes

Remove the parentheses after the method name in order to provide the method group (which can be evaluated to a delegate). As it is you're invoking the method and providing the result of that method, which doesn't match the delegate that is expected.

var result = InvokerHelper.Invoker(_content.GetContents);