Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在VS2010中创建自定义调试器可视化工具来可视化不可序列化的类?_C#_Visual Studio 2010_Debuggervisualizer - Fatal编程技术网

C# 如何在VS2010中创建自定义调试器可视化工具来可视化不可序列化的类?

C# 如何在VS2010中创建自定义调试器可视化工具来可视化不可序列化的类?,c#,visual-studio-2010,debuggervisualizer,C#,Visual Studio 2010,Debuggervisualizer,我想在VS2010中使用自定义调试器可视化工具可视化对象。但该类的对象没有可序列化属性。 由于源代码是长期编写和维护的,因此我不想将该类更改为仅用于调试器可视化工具目的的可序列化类 有人能告诉我如何实现这一点吗?无法使用BinaryFormatter,因为它希望将类标记为可序列化。但是,您可以使用不同的序列化程序,如: 我也有同样的问题,因为我不想改变我的整个对象模型,所以我编写了一个助手库,为所有类型(不包括抽象类型、接口和非公共类型)在给定程序集上注入序列化属性。如果您想继续使用Bi

我想在VS2010中使用自定义调试器可视化工具可视化对象。但该类的对象没有可序列化属性。 由于源代码是长期编写和维护的,因此我不想将该类更改为仅用于调试器可视化工具目的的可序列化类


有人能告诉我如何实现这一点吗?

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

我也有同样的问题,因为我不想改变我的整个对象模型,所以我编写了一个助手库,为所有类型(不包括抽象类型、接口和非公共类型)在给定程序集上注入序列化属性。如果您想继续使用BinaryFormatter,请尝试使用该库,可以在生成后使用MSBuild任务来完成


如果您有兴趣,可以参考MSDN指南,了解如何编写MSBuild任务,但无法使用BinaryFormatter,因为它希望将类标记为可序列化。但是,您可以使用不同的序列化程序,如:

我也有同样的问题,因为我不想改变我的整个对象模型,所以我编写了一个助手库,为所有类型(不包括抽象类型、接口和非公共类型)在给定程序集上注入序列化属性。如果您想继续使用BinaryFormatter,请尝试使用该库,可以在生成后使用MSBuild任务来完成


如果您感兴趣,这里有一个MSDN指南,介绍如何编写MSBuild任务,我在这里附上了代码,使用Newtonsoft的Json.net实现了我在问题中提出的要求

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

}

我在这里附上了代码,我使用Newtonsoft的Json.net实现了我在问题中提出的要求

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

}

是的,我承认我们不能用BinaryFormatter来实现这一点。我将尝试使用JSON.NET,因为我们在项目中使用它。嗨,我能够使用JSON.NET序列化没有[serializable]属性的对象。我终于得到了我真正想要的。谢谢你的帮助,不客气。但是,如果您的对象变得非常复杂(循环引用,10-15个嵌套级别),JSON.NET可能会变慢。是的,我承认我们不能用BinaryFormatter实现这一点。我将尝试使用JSON.NET,因为我们在项目中使用它。嗨,我能够使用JSON.NET序列化没有[serializable]属性的对象。我终于得到了我真正想要的。谢谢你的帮助,不客气。但是,如果对象变得非常复杂(循环引用,10-15个嵌套级别),JSON.NET可能会变得很慢。