Is there a way to avoid overloading a method with Func<T, ...> argument for each Func parameter count

StackOverflow https://stackoverflow.com/questions/20827928

Вопрос

I know the question sounds a bit wierd. Sorry for that, having difficulties trying to get where i want to and even explain it. To make it simple, i have a method with Func<T> argument. But i do not always pass a parameterless Action to that method, i need varying numbers of parameters and i'm trying to find a way to avoid overloading my method everytime number of parameters needed is increased

Here is my generic class, I need to overload GetInstance method:

public class MethodResult<T>
{
    public T Result { get; set; }
    public bool IsResulted { get; set; }
    public Exception Error { get; set; }

    private MethodResult() { }

    public static MethodResult<T> GetInstance<T>(Func<T> method)
    {
        MethodResult<T> obj = new MethodResult<T>();

        try
        {
            obj.Result = method();
            obj.IsResulted = true;
            obj.Error = null;
        }
        catch (Exception ex)
        {
            obj.Result = default(T);
            obj.IsResulted = false;
            obj.Error = ex;
        }

        return obj;
    }

    public static MethodResult<T> GetInstance<T, T1>(Func<T1, T> method, T1 param1)
    {
        MethodResult<T> obj = new MethodResult<T>();

        try
        {
            obj.Result = method(param1);
            obj.IsResulted = true;
            obj.Error = null;
        }
        catch (Exception ex)
        {
            obj.Result = default(T);
            obj.IsResulted = false;
            obj.Error = ex;
        }

        return obj;
    }
}

And here is sample showing how i want to make use of it:

public static void Main(string[] args)
{            
        var x = MethodResult<int>.GetInstance(IntResult, 5);
        Console.WriteLine("Result: {0}, IsResulted: {1}, ErrorMessage: {2}", x.Result, x.IsResulted, (x.Error == null ? "null" : x.Error.Message));

        var y = MethodResult<string>.GetInstance(SayHello);
        Console.WriteLine("Result: {0}, IsResulted: {1}, ErrorMessage: {2}", y.Result, y.IsResulted, (y.Error == null ? "null" : y.Error.Message));

        Console.Read();
}

public static int IntResult(int x) { return x + 1; }
public static int IntResult(int x, int y) { return x + y; }
public static string SayHello() { return "Hello world!"; }

In order to be able to use IntResult(int x, int y) i have to overload GetInstance method with signiture:

public static MethodResult<T> GetInstance<T, T1, T2>(Func<T1, T2, T> method, T1 param1, T2 param2)

It's obvious that this will become very time consuming as it's already been annoying. Is there a way to avoid that much overloading ?

Это было полезно?

Решение

Instead of passing the function to call and the parameters along with it, you can pass a parameterless anonymous delegate calling the method you want to point to with parameters you want. Just remove any overloads of GetInstance just keep:

public static MethodResult<T> GetInstance(Func<T> method)
{
    MethodResult<T> obj = new MethodResult<T>();

    try
    {
        obj.Result = method();
        obj.IsResulted = true;
        obj.Error = null;
    }
    catch (Exception ex)
    {
        obj.Result = default(T);
        obj.IsResulted = false;
        obj.Error = ex;
    }

    return obj;
}

And then call it as following:

int n = 1;
var x = MethodResult<string>.GetInstance(() => SayHello());
var y = MethodResult<string>.GetInstance(() => IntResult(2));
var z = MethodResult<int>.GetInstance(() => IntResult(n, 9));
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top