Skip to main content
edited tags; edited title; deleted 94 characters in body
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Better way to make a generic Generic function that accepts both Actions and Func's as parameters

I've created a generic try catch I can apply throughout my code so I'm not repeating myself.

    private HttpResponseMessage TryCatch(Action action)
    {
        return TryCatch(() => { action(); return "ActionToFunc"; });
    }

    private HttpResponseMessage TryCatch<T>(Func<T> func)
    {
        try
        {
            var result = func();

            if (result.ToString() == "ActionToFunc")
            {
                return Request.CreateResponse(HttpStatusCode.OK);
            }

            return Request.CreateResponse(HttpStatusCode.OK, result);
        }
        catch (ArgumentNullException)
        {
            return Request.CreateResponse(HttpStatusCode.NotFound);
        }
        catch (Exception ex)
        {
            return Request.CreateResponse(HttpStatusCode.InternalServerError);
        }
    }

I can call it using:

TryCatch(() => myFunction(parameter));

Is there a better way of doing achieving the same result?

Better way to make a generic function that accepts both Actions and Func's as parameters

I've created a generic try catch I can apply throughout my code so I'm not repeating myself.

    private HttpResponseMessage TryCatch(Action action)
    {
        return TryCatch(() => { action(); return "ActionToFunc"; });
    }

    private HttpResponseMessage TryCatch<T>(Func<T> func)
    {
        try
        {
            var result = func();

            if (result.ToString() == "ActionToFunc")
            {
                return Request.CreateResponse(HttpStatusCode.OK);
            }

            return Request.CreateResponse(HttpStatusCode.OK, result);
        }
        catch (ArgumentNullException)
        {
            return Request.CreateResponse(HttpStatusCode.NotFound);
        }
        catch (Exception ex)
        {
            return Request.CreateResponse(HttpStatusCode.InternalServerError);
        }
    }

I can call it using:

TryCatch(() => myFunction(parameter));

Is there a better way of doing achieving the same result?

Generic function that accepts both Actions and Func's as parameters

I've created a generic try catch I can apply throughout my code so I'm not repeating myself.

private HttpResponseMessage TryCatch(Action action)
{
    return TryCatch(() => { action(); return "ActionToFunc"; });
}

private HttpResponseMessage TryCatch<T>(Func<T> func)
{
    try
    {
        var result = func();

        if (result.ToString() == "ActionToFunc")
        {
            return Request.CreateResponse(HttpStatusCode.OK);
        }

        return Request.CreateResponse(HttpStatusCode.OK, result);
    }
    catch (ArgumentNullException)
    {
        return Request.CreateResponse(HttpStatusCode.NotFound);
    }
    catch (Exception ex)
    {
        return Request.CreateResponse(HttpStatusCode.InternalServerError);
    }
}

I can call it using:

TryCatch(() => myFunction(parameter));

Is there a better way of doing achieving the same result?

Source Link

Better way to make a generic function that accepts both Actions and Func's as parameters

I've created a generic try catch I can apply throughout my code so I'm not repeating myself.

    private HttpResponseMessage TryCatch(Action action)
    {
        return TryCatch(() => { action(); return "ActionToFunc"; });
    }

    private HttpResponseMessage TryCatch<T>(Func<T> func)
    {
        try
        {
            var result = func();

            if (result.ToString() == "ActionToFunc")
            {
                return Request.CreateResponse(HttpStatusCode.OK);
            }

            return Request.CreateResponse(HttpStatusCode.OK, result);
        }
        catch (ArgumentNullException)
        {
            return Request.CreateResponse(HttpStatusCode.NotFound);
        }
        catch (Exception ex)
        {
            return Request.CreateResponse(HttpStatusCode.InternalServerError);
        }
    }

I can call it using:

TryCatch(() => myFunction(parameter));

Is there a better way of doing achieving the same result?