我喜欢使用隐式打字几乎所有东西,因为它干净简单。但是,当我需要在单个语句周围包装try ... catch块时,我必须打破隐式类型以确保变量具有已定义的值。这是一个人为的假设例子:
var s = "abc";
// I want to avoid explicit typing here
IQueryable<ABC> result = null;
try {
result = GetData();
} catch (Exception ex) { }
if (result != null)
return result.Single().MyProperty;
else
return 0;
有没有办法可以通过异常处理调用GetData()
,但无需明确定义结果变量的类型?像GetData().NullOnException()
?
答案 0 :(得分:9)
这是一个常见问题。我建议您坚持使用现有的解决方案。
如果你真的想要一个替代方案,那就是:
static T NullOnException<T>(Func<T> producer) where T : class {
try { return producer(); } catch { return null; } //please modify the catch!
}
//now call it
var result = NullOnException(() => GetData());
请修改此项以记录异常或将catch限制为具体类型。我并不认可吞下所有例外情况。
由于这个答案被大量阅读,我想指出这个实现只是演示质量。在生产代码中,您可能应合并评论中提供的建议。给自己写一个强大的,精心设计的辅助功能,可以为你提供多年的服务。
答案 1 :(得分:2)
将代码放在try:
中var s = "abc";
// I want to avoid explicit typing here
try {
var result = GetData();
if (result != null)
return result.Single().MyProperty;
else
return 0;
} catch (Exception ex) { }
答案 2 :(得分:0)
我找到了与@usr类似的解决方案,但语义略有不同:
T LiftScope<T>(Func<T> ScopedFunction)
{
T result = ScopedFunction();
return result;
}
LiftScope
的目的是将内部变量传递给调用者,而不会影响隐式类型。这可以用来解决原始问题,但try...catch
实际上会嵌入到调用中。
try...catch
var result =
LiftScope(() => {
try { return producer(); } catch { return null; }
});
现在调用者能够负责异常处理。此外,这可以在一些类似的用例中使用,这些用例的范围很短。
if
var result =
LiftScope(() => {
if (a == b)
return GetData(true);
else if (b == c)
return GetData(false);
else
return GetData(true, 2);
});
这也可以通过三元式if
语句来解决。
using
var result =
LiftScope(() => {
using (var myContext = new MyDataContext())
{
return myContext.MyTable.Where(w => w.A == B).ToList();
}
});