簡體   English   中英

DbCommand的自定義Visualizer

[英]Custom Visualizer for DbCommand

您好我試圖為應在Visual Studio 2013中使用的DbCommand對象創建自定義Visualizer。

我有以下代碼

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對象並在需要多個屬性時將其設置為seralize):

[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());
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM