我可能不理解的問題,而是實際上突破上線(似乎不可能),將表達式樹中的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)
你的意思是在這些方法中的一種堅持一個斷點在lambda表達式的一部分? –
不知道是否[這有助於],但我現在正在進行搜索因爲這很有趣。所有的道路似乎都要去LinqPad。我看不出如何在沒有源代碼的情況下斷開它,然後當你有源代碼時,你能不能在負責'Max'的提供程序部分斷點? –
[表達式樹](http://msdn.microsoft.com/en-us/library/bb397951.aspx)MSDN部分還有[調試表達式樹]一節(http://msdn.microsoft.com/ EN-US /庫/ ee725345)。我認爲這是我必須停止搜索並重新開始工作的地方;-) –