【发布时间】:2014-06-09 02:53:56
【问题描述】:
我想写一个扩展方法来运行某个对象的方法,并返回执行过程中发生的异常(如果有的话)。
换句话说,将 myObject.Foo() 的动态设置为 anyObject.AnyMethod(...)。
Exception incurredException = null;
try
{
myObject.Foo();
}
catch(Exception e)
{
incurredException = e;
}
return incurredException;
到这里:
Exception e = IncurredException( () => myObject.Foo() );
我不知道 Func、Expression、Delegate 等在这里是否合适。想法?谢谢!
【问题讨论】:
-
您在那里做的事情将是最简单的
Action。如果方法有返回类型,你想做什么?
标签: c# .net lambda delegates func