1

I want to visualize an object with a custom Debugger Visualizer in VS2010. But the object of that class does not have Serializable property. Since the Source code is written and maintained for long, I don't want to change the class to Serializable for Debugger Visualizer purpose only.

Can someone tell me how to achieve this?

4

2 回答 2

3

I am here attaching the code with which i achieved what i asked in the question using Json.net from 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

}
于 2013-04-30T06:30:19.820 回答
2

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

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

如果你对这里感兴趣,这里有一个关于如何编写 MSBuild 任务的 MSDN 指南

于 2013-04-19T09:39:04.817 回答