[英]How the Visual Studio debugger infer Value inside watch window?
例如,在監視窗口中,我看到:
名稱 :任務
值 :Id = 11,狀態= WaitingToRun,方法=“無效<
RetreiveFromCache>
b__0()”
類型 :System.Threading.Tasks.Task
因此, task
變量的類型為Task,我想訪問調試器打印的相同值,特別是我對Method
值( RetreiveFromCache )感興趣
但是在task
變量上,我只能訪問.Id,.Status等...,而不能訪問.Method
Method
值從何而來?
調試器如何“推斷” Method
值?
您可以覆蓋ToString()方法。 這是簡單的方法,但是您正在尋找的是以下類:
有關更多信息,請參見https://msdn.microsoft.com/es-es/library/ms228992(v=vs.110).aspx
Task
對象的Action
變量不可訪問。 您可以做的是創建自己的類,該類擴展Task
的功能,同時公開Action(該操作包含有關要運行的方法的信息)。
一個簡單的例子:
public class CustomTask : Task
{
private Action m_action;
public Action Action
{
get { return m_action; }
}
public CustomTask(Action action) : base(action)
{
m_action = action;
}
}
然后在您的代碼中,可以像這樣使用它:
CustomTask ct = new CustomTask(MyMethod);
Console.WriteLine("ID: {0}, Method: {1}, Status: {2}", ct.Id, ct.Action.Method, ct.Status);
//Prints: "ID: 1, Method: Void MyMethod(), Status: Created"
ct.Start();
Console.WriteLine("ID: {0}, Method: {1}, Status: {2}", ct.Id, ct.Action.Method, ct.Status);
//Prints: "ID: 1, Method: Void MyMethod(), Status: WaitingToRun"
ct.Wait();
Console.WriteLine("ID: {0}, Method: {1}, Status: {2}", ct.Id, ct.Action.Method, ct.Status);
//Prints: "ID: 1, Method: Void MyMethod(), Status: RanToCompletion"
請注意,要訪問有關該方法的信息,您必須訪問ct.Action.Method
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.