我試圖讓VisualRudio 2010的DataRow [] DebuggerVisualizer工作,不幸的是我無法獲得它的工作。我可以讓DataRow工作,但不能DataRow [],我會喜歡任何請嗎?試圖讓DataRow []調試器展示臺在Visual Studio 2010中工作
代碼的肉在這裏。
[assembly: DebuggerVisualizer(
typeof(PCHenry.DR),
typeof(PCHenry.DRObjectSource),
Target = typeof(DataRow[]),
Description = "DataRow Array Debugger Visualizer (or so if you see this then it's working YAHOO!)")]
namespace PCHenry
{
public class DR : DialogDebuggerVisualizer
{
protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
{
StringBuilder stringToDebug = new StringBuilder();
using(Stream dataStream = objectProvider.GetData())
{
BinaryFormatter formatter = new BinaryFormatter();
string incomingData = formatter.Deserialize(dataStream) as string;
stringToDebug.Append(string.Format("*!!!!{0}!!!!*", incomingData));
}
MessageBox.Show(stringToDebug.ToString(), "PCH String Debugger Visualizer", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
public class DRObjectSource : VisualizerObjectSource
{
public override void GetData(object target, Stream outgoingData)
{
if(target != null && target is DataRow[])
{
DataRow[] rows = target as DataRow[];
BinaryFormatter formatter = new BinaryFormatter();
//formatter.Serialize(outgoingData, target);
formatter.Serialize(outgoingData, string.Format("There are {0} rows of data", rows.Length));
}
}
}
}
正如我希望你能看到的,我試圖正確設置目標,但它沒有在運行時/調試時被VS使用。是的,我正在將DLL複製到正確的Visualizers目錄。實際上,我正在使用BuildEvent爲我完成這項工作。
xcopy "$(SolutionDir)$(ProjectName)\$(OutDir)$(TargetFileName)" "$(USERPROFILE)\Documents\Visual Studio 2010\Visualizers" /y
當我測試這個,我用這個。
static void Main(string[] args)
{
//String myName = "Peter Henry";
#region DataSetup, create a Habs DataTable and populate it with players
DataTable table = new DataTable("Habs");
table.Columns.Add("PlayerNumber", typeof(Int32));
table.Columns.Add("PlayerName", typeof(string));
table.Columns.Add("Position", typeof(string));
//team as current as 09-23-2010 from the Canadiens! GO HABS GO!
table.Rows.Add(new object[] { 32, "Travis Moen", "F" });
table.Rows.Add(new object[] { 94, "Tom Pyatt", "F" });
table.Rows.Add(new object[] { 75, "Hal Gill", "D" });
table.Rows.Add(new object[] { 26, "Josh Gorges", "D" });
table.Rows.Add(new object[] { 76, "P.K. Subban", "D" });
table.Rows.Add(new object[] { 35, "Alex Auld", "G" });
#endregion
//use this to show the debugger in two different ways
DataRow[] defencemen = table.Select("Position = 'D'", "PlayerNumber");
//this proves this works when told which ObjectSource to use
VisualizerDevelopmentHost host = new VisualizerDevelopmentHost(
defencemen, typeof(PCHenry.DR),
typeof(PCHenry.DRObjectSource));
host.ShowVisualizer();
//but when I try to use VS debugging here, it can't seem to find the custom DebuggerVisualizer as I would expect
defencemen = table.Select("Position = 'D'", "PlayerNumber");
Debugger.Break();
Console.WriteLine("FIN");
Console.ReadLine();
}
這裏的關鍵是,在VisualizerDevelopmentHost正常運行,並且因爲它是告知使用該VisualizerObjectSource我只能猜測。但是當我點擊Debugger.Break();並嘗試像正常使用它,我無法看到防守者DataRow []的放大鏡。
我相信我的心底可以做到。我讀了MSDN DataRow無法完成,但我得到它的工作。我真的希望你能幫助我在那裏工作。
非常感謝你們的回覆。你證實了我的想法(好吧,我在與它共同戰鬥四夜之後才意識到)!再次感謝。我在博客上發表了博文,並引用了這些信息。非常感謝您的寶貴時間。
Visual Studio Debugger Visualizers (Take Three)
這是一個有趣的黑客。我從來沒有想過在觀察窗口中嘗試創建一個對象。因此,您可以爲列表編寫一個可視化工具,並且在觀察窗口中,您可以觀看「新列表(yourDataRowArray)」,並可視化,無需更改代碼。這很整齊。另外,我有幾年前的模糊記憶,當時我正在嘗試編寫一個double []的可視化工具,我嘗試了IEnumerable並沒有得到任何地方。 –
Spike
2010-09-29 00:36:10
這是有道理的,因爲可視化器在對象和接口上工作不是對象。我剛試過Fish f = new Fish(); IFish if = f;和一個可視化工具Targeting Fish在f上工作,如果,但是一個針對IFish的工作都不行。 – Spike 2010-09-29 01:44:50