簡體   English   中英

如何在已編譯的表達式樹中調試或設置break語句?

[英]How can I debug or set a break statement inside a compiled expression tree?

當外部庫包含LINQ提供程序時,它在執行動態表達式樹時拋出異常,如何在拋出該表達式時中斷?

例如,我使用第三方LINQ2CRM提供程序,它允許我調用IQueryableMax<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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM