Estoy tratando de encontrar una manera para emitir automáticamente algo que una acción o Func y lo mejor que se puede llegar a decir algo como esto:¿Por qué no puedo convertir implícitamente un delegado con métodos de extensión?
[TestFixture]
public class ExecutionTest
{
public void BadMethod()
{
throw new Exception("Something bad happened");
}
[Test]
public void TestBadMethod()
{
// Want this, but it won't work!!
// BadMethod.Execute().IgnoreExceptions();
// Ick
((Action)BadMethod).Exec().IgnoreExceptions();
// Still ick
((Action)BadMethod).IgnoreExceptions();
// Do not want
ExtensionMethods.Exec(BadMethod).IgnoreExceptions();
// Better but still meh
this.Exec(BadMethod).IgnoreExceptions();
}
}
public static class ExtensionMethods
{
public static Action Exec(this Action action)
{ return action; }
public static Action Exec(this object obj, Action action)
{ return action; }
public static void IgnoreExceptions(this Action action)
{
try { action(); }
catch {}
}
}
Tiene que una manera mejor/más fácil de hacer esto, algún pensamiento?
No estoy seguro de que sea un problema de sobrecarga de métodos porque el compilador decidirá a cuál llamar. Pero esta es la mejor respuesta que he visto hasta ahora ... –