gpt4 book ai didi

c# - DbCommand 的自定义可视化工具

转载 作者:行者123 更新时间:2023-11-30 15:27:29 25 4
gpt4 key购买 nike

您好,我正在尝试为应该在 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.

我的理解是,通过使用自定义 VisualizerObjectSource 我可以解决序列化问题?

作为旁注,我尝试将 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());
}
}
}

关于c# - DbCommand 的自定义可视化工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27508601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com