如何在VS2010中创建自定义Debugger Visualizer以可视化非Serializable的类?

时间:2013-04-19 09:20:58

标签: c# visual-studio-2010 debuggervisualizer

我想在VS2010中使用自定义Debugger Visualizer可视化对象。但该类的对象没有Seri​​alizable属性。 由于源代码的编写和维护时间很长,我不希望将类更改为Serializable for Debugger Visualizer目的。

有人可以告诉我如何实现这个目标吗?

2 个答案:

答案 0 :(得分:3)

我在这里附上我使用J92.net从Newtonsoft获得我在问题中提出的代码。

namespace MyCustomVisualizer
{
    public class MyObjectSource : VisualizerObjectSource
    {
        public override void GetData(object target, Stream outgoingData)
        {
            try
            {
                byte[] byteArray = JsonHelper.Serialize(target);
                outgoingData.Write(byteArray, 0, byteArray.Length);
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message, "VisualizerObjectSource Error");
            }
        }
    }

    public class MyVisualizer : DialogDebuggerVisualizer
    {
        override protected void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
        {
            try
            {
                string _str = null;
                Stream _stream = objectProvider.GetData();

                if (_stream != null && _stream.Length > 0)
                {
                    object _obj = null;
                    _obj = JsonHelper.Deserialize(_stream); // Here i get the object i want
                    // ^^^
                    // Now add ur code to visualize the object in your way.

                    /* This is only to verify the object data before visualizing.
                    if (_obj != null)
                    {
                        _str = JsonHelper.ObjectToString(_obj);
                        MessageBox.Show(_str, "Show");
                    }
                    */
                }

                // Show the grid with the list
                windowService.ShowDialog();
            }
            catch (Exception exp) { MessageBox.Show(exp.Message, "Visualizer Error"); }
            finally
            {
            }
        }
    }

    #region JsonHelper Class
    public static class JsonHelper
    {
        public static byte[] Serialize(object _Object)
        {
            MemoryStream _MemoryStream = new MemoryStream();
            JsonSerializer serializer = new JsonSerializer();
            serializer.NullValueHandling = NullValueHandling.Ignore;
            serializer.TypeNameHandling = TypeNameHandling.Auto;

            try
            {
                using (StreamWriter sw = new StreamWriter(_MemoryStream))
                using (JsonWriter writer = new JsonTextWriter(sw))
                {
                    serializer.Serialize(writer, _Object);
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message, "Serialize Error");
            }
            return _MemoryStream.ToArray();
        }

        public static object Deserialize(Stream _ByteArray)
        {
            Object _object = new Object();
            JsonSerializer serializer = new JsonSerializer();
            serializer.NullValueHandling = NullValueHandling.Ignore;
            serializer.TypeNameHandling = TypeNameHandling.Auto;
            try
            {
                StreamReader sw = new StreamReader(_ByteArray);
                JsonReader reader = new JsonTextReader(sw);
                _object = serializer.Deserialize(reader);
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message, "Deserialize Error");
            }
            return _object;
        }

        public static string ObjectToString(object _object)
        {
            string _str = string.Empty;
            JsonSerializerSettings _jsonSerializeSettings = new JsonSerializerSettings();
            _jsonSerializeSettings.NullValueHandling = NullValueHandling.Ignore;
            _jsonSerializeSettings.TypeNameHandling = TypeNameHandling.Auto;
            _str = JsonConvert.SerializeObject(_object, Newtonsoft.Json.Formatting.Indented, _jsonSerializeSettings);
            return _str;
        }
    }
    #endregion

}

答案 1 :(得分:2)

没有办法处理BinaryFormatter,因为它希望将类标记为可序列化。但是,您可以使用不同的序列化程序,如:

我遇到了同样的问题,因为我不想改变我的整个对象模型,我编写了一个帮助库SInject,它在给定程序集上为所有类型注入序列化属性,不包括摘要类型,接口和非公共类型。如果您想坚持使用BinaryFormatter尝试该库,可以在构建之后使用MSBuild任务完成。

如果您感兴趣here,有关于如何编写MSBuild任务的MSDN指南