[英]How can I debug or set a break statement inside a compiled expression tree?
当外部库包含LINQ提供程序时,它在执行动态表达式树时抛出异常,如何在抛出该表达式时中断?
例如,我使用第三方LINQ2CRM提供程序,它允许我调用IQueryable
的Max<TSource, TResult>()
方法,但是当它抛出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)
我可能没有理解这个问题,但是实际上并没有突破(似乎不可能),在表达式树中放置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);
}
控制台输出:
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 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.