Abe*_*bel 12 c# linq debugging expression-trees
当外部库包含LINQ提供程序时,它在执行动态表达式树时抛出异常,如何在抛出该表达式时中断?
例如,我使用第三方LINQ2CRM提供程序,它允许我调用Max<TSource, TResult>()方法IQueryable,但是当它抛出一个时InvalidCastException,我不能在抛出异常时当场破坏,因此很难查看堆栈跟踪,因为它是调试器在我的代码中破坏它时已经解除了.我为上述例外设置了"中断".我的调试设置是:

澄清我想要打破的确切位置.我不希望在一侧的LINQ表达突破,而是,我希望在表达式树被执行,或者,把换句话说打破,当IQueryable扩展方法Max()调用由LINQ提供者提供的覆盖.堆栈跟踪的顶部看起来像这样,这是我想要在内部(或通过,或其他)的地方:
at XrmLinq.QueryProviderBase.Execute[T](Expression expression)
at System.Linq.Queryable.Max[TSource,TResult](IQueryable`1 source, Expression`1 selector)
Run Code Online (Sandbox Code Playgroud)
我可能没有理解这个问题,但是实际上并没有突破(似乎不可能),在表达式树中放置try-catch并记录异常就足够了吗?
static void Main(string[] args)
{
var logExceptionMethod = typeof (Program).GetMethod("LogException", BindingFlags.Static | BindingFlags.NonPublic);
var createFileMethod = typeof (System.IO.File).GetMethod("Create", new[] {typeof(string)});
// Parameter for the catch block
var exception = Expression.Parameter(typeof(Exception));
var expression =
Expression.TryCatch(
Expression.Block(typeof(void),
// Try to create an invalid file
Expression.Call(createFileMethod, Expression.Constant("abcd/\\"))),
// Log the exception from the catch
Expression.Catch(exception, Expression.Call(logExceptionMethod, exception)));
Expression.Lambda<Action>(expression).Compile()();
}
static void LogException(Exception ex)
{
Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
}
Run Code Online (Sandbox Code Playgroud)
控制台输出:
The filename, directory name, or volume label syntax is incorrect.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.File.Create(String path)
at lambda_method(Closure )
Run Code Online (Sandbox Code Playgroud)