gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:16:56 24 4
gpt4 key购买 nike

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

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

最佳答案

我在这里附上代码,我使用 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

}

关于c# - 如何在 VS2010 中创建自定义调试器可视化工具以可视化不可序列化的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16101310/

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