用于 DbCommand 的自定义可视化工具
本文关键字:可视化 工具 自定义 DbCommand 用于 | 更新日期: 2023-09-27 18:34:31
>嗨,我正在尝试为应在Visual Studio 2013中使用的DbCommand对象创建自定义可视化工具。
我有以下代码
using VisualizerTest;
using Microsoft.VisualStudio.DebuggerVisualizers;
using System;
using System.Data.Common;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
[assembly: DebuggerVisualizer(typeof(TestVisualizer), typeof(CommandObjectSource), Target = typeof(DbCommand), Description = "Test")]
namespace VisualizerTest
{
public class TestVisualizer : DialogDebuggerVisualizer
{
protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
{
DbCommand command;
try
{
using (Stream stream = objectProvider.GetData())
{
BinaryFormatter formatter = new BinaryFormatter();
command = (DbCommand)formatter.Deserialize(stream);
}
MessageBox.Show(command.CommandText);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
namespace VisualizerTest
{
[Serializable]
public class CommandObjectSource : VisualizerObjectSource
{
public override void GetData(object target, Stream outgoingData)
{
if (target != null && target is DbCommand)
{
DbCommand command = (DbCommand)target;
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(outgoingData, command);
}
}
}
}
但是从未调用过CommandObjectSource
,而是我得到了一个例外
Microsoft.VisualStudio.DebuggerVisualizers.DebugViewerShim.RemoteObjectSourceException: Type 'System.Data.SqlClient.SqlCommand' in Assembly 'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.
我的理解是,通过使用自定义可视化器对象源,我将解决序列化问题?
作为旁注,我试图将Target = typeof(DbCommand)
更改为Target = typeof(SqlCommand)
,这没有任何区别。
测试代码:
class Program
{
static void Main(string[] args)
{
using (SqlCommand command = new SqlCommand("SELECT Field1 FROM table WHERE Field2 = @Value1"))
{
command.Parameters.AddWithValue("@Value1", 1338);
TestValue(command);
}
Console.ReadKey();
}
static void TestValue(object value)
{
VisualizerDevelopmentHost visualizerHost = new VisualizerDevelopmentHost(value, typeof(TestVisualizer));
visualizerHost.ShowVisualizer();
}
}
因为您显式创建VisualizerDevelopmentHost
所以它不会使用该DebuggerVisualizerAttribute
,因此您必须将CommandObjectSource
作为第三个参数传入:
VisualizerDevelopmentHost visualizerHost =
new VisualizerDevelopmentHost(value, typeof(TestVisualizer),
typeof(CommandObjectSource));
通过此更改,您的CommandObjectSource
将被调用,但您仍然遇到序列化问题,因为BinaryFormatter
还需要将类标记为Seralizabe
...
因此,您可能应该只包含CommandText
(或者创建一个新的 DTO 对象并在需要多个属性时将其锯齿化(:
[Serializable]
public class CommandObjectSource : VisualizerObjectSource
{
public override void GetData(object target, Stream outgoingData)
{
if (target != null && target is DbCommand)
{
DbCommand command = (DbCommand)target;
var writer = new StreamWriter(outgoingData);
writer.WriteLine(command.CommandText);
writer.Flush();
}
}
}
并阅读它:
public class TestVisualizer : DialogDebuggerVisualizer
{
protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
{
string command;
try
{
command = new StreamReader(objectProvider.GetData()).ReadLine();
MessageBox.Show(command);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}