您可以创建一组辅助方法来包装拉姆达与: -
public static class Helper
{
public static AsyncCallback GetAsyncCallback(Action<IAsyncResult> inner)
{
return (a) =>
{
try
{
inner(a);
}
catch (Exception err)
{
// Your handling for "uncaught" errors
}
};
}
public static Action GetAction(Action inner)
{
return() =>
{
try
{
inner();
}
catch (Exception err)
{
// Your handling for "uncaught" errors
}
};
}
public static Action<T> GetAction(Action<T> inner)
{
return (a) =>
{
try
{
inner(a);
}
catch (Exception err)
{
// Your handling for "uncaught" errors
}
};
}
// and so on also:-
public static Func<T> GetFunc(Func<T> inner;)
{
return() =>
{
try
{
return inner();
}
catch (Exception err)
{
// Your handling for "uncaught" errors
}
};
}
public static Func<T1, TReturn> GetFunc(Func<T1, TReturn> inner;)
{
return (a) =>
{
try
{
return inner(a);
}
catch (Exception err)
{
// Your handling for "uncaught" errors
}
};
}
}
现在你可以用拉姆达的,不用担心默认样板异常处理: -
this.Context.BeginSaveChanges(Helper.GetAsyncCallback((ar) =>
{
// Only needs specific exception handling or none at all
}), null);