c# - How to create a custom Debugger Visualizer in VS2010 to visualize a class which is non-Serializable? -
i want visualize object custom debugger visualizer in vs2010. object of class not have serializable property. since source code written , maintained long, don't want change class serializable debugger visualizer purpose only.
can tell me how achieve this?
i here attaching code achieved asked in question using json.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 object want // ^^^ // add ur code visualize object in way. /* verify object data before visualizing. if (_obj != null) { _str = jsonhelper.objecttostring(_obj); messagebox.show(_str, "show"); } */ } // show grid list windowservice.showdialog(); } catch (exception exp) { messagebox.show(exp.message, "visualizer error"); } { } } } #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 }
Comments
Post a Comment