Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace System.Diagnostics {
- using System;
- using System.CodeDom.Compiler;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Collections;
- #region ObjectWriter class
- public static class ObjectWriter {
- public const int DEFAULT_MAXDEPTH = 6;
- static int _maxDepth = DEFAULT_MAXDEPTH;
- public static int MaxDepth {
- get { return _maxDepth; }
- set {
- _maxDepth = Math.Max(1,value);
- }
- }
- /// <summary>
- /// Traces an object's properties using reflection
- /// </summary>
- /// <param name="obj">The object to be traced</param>
- /// <param name="builder">Optional StringBuilder to append the trace to</param>
- public static void TraceObject(object obj,int depth = -1,StringBuilder builder = null,bool html = false,string tabString = null) {
- if(obj == null)
- throw new ArgumentNullException("obj");
- builder = builder ?? new StringBuilder();
- WriteObject(obj,depth,builder,html,tabString);
- Trace.Write(builder.ToString());
- }
- /// <summary>
- /// Debugs an object's properties using reflection
- /// </summary>
- /// <param name="obj">The object to be debugged</param>
- /// <param name="builder">Optional StringBuilder to append the debug to</param>
- public static void DebugObject(object obj,int depth = -1,StringBuilder builder = null,bool html = false,string tabString = null) {
- if(obj == null)
- throw new ArgumentNullException("obj");
- builder = builder ?? new StringBuilder();
- WriteObject(obj,depth,builder,html,tabString);
- Debug.Write(builder.ToString());
- }
- public static string WriteObjectString(object obj,int depth = -1,bool html = false) {
- StringBuilder builder = new StringBuilder();
- WriteObject(obj,depth,builder,html);
- return builder.ToString();
- }
- /// <summary>
- /// Writes the object's properties to a StringBuilder using reflection
- /// </summary>
- /// <param name="obj">The object to be written</param>
- /// <param name="builder">Optional StringBuilder to append the trace to</param>
- public static void WriteObject(object obj,int depth = -1,StringBuilder builder = null,bool html = false,string tabString = null) {
- if(obj == null)
- throw new ArgumentNullException("obj");
- builder = builder ?? new StringBuilder();
- tabString = tabString ?? IndentedTextWriter.DefaultTabString;
- if(html) {
- tabString = "<span class='t'> </span>";
- }
- using(IndentedTextWriter writer = new IndentedTextWriter(new StringWriter(builder),tabString)) {
- if(html) {
- writer.NewLine = "<br/>" + Environment.NewLine;
- writer.Write(
- @"
- <style type='text/css'>
- .output {line-height:12px;}
- .t {margin-right:10px;}
- fieldset {margin:10px; padding:10px;border: .5px solid red;}
- legend {}
- fieldset ul {}
- fieldset ul li{}
- fieldset table {margin:0px; margin-top:0px; width:80%;}
- td.pn {width:30%;text-align:left;}
- td.pv {width:80%;text-align:left;color:darkgreen;}
- fieldset h2 {font-weight:bold;font-size:16px;display:inline;color:blue;}
- </style>
- ");
- }
- writer.WriteLine("<div class='output'>");
- WriteObjectInternal(obj,writer,depth,html);
- if(html) {
- writer.WriteLine("</div>");
- }
- }
- }
- /// <summary>
- /// Writes an object's properties using reflection
- /// </summary>
- /// <param name="obj">The object to be traced</param>
- /// <param name="builder">Optional StringBuilder to append the trace to</param>
- static void WriteObjectInternal(object obj,IndentedTextWriter writer,int depth = -1,bool html = false) {
- if(depth == -1)
- depth = MaxDepth;
- if(obj == null || writer.Indent >= depth)
- return;
- string start_format = "START_OBJECT -> {0} | {1}";
- string end_format = "END_OBJECT -> {0} | {1}";
- string property_start = null;
- string property_end = null;
- string property_format = @"{0,-20} : {1,-20}";
- string null_value = "[null]";
- if(html) {
- string start_format_html = "<fieldset class='fs_" + writer.Indent + "'><legend><h2>{0}</h2> [hash: {1}]</legend>";
- string end_format_html = "<p><h2>{0}</h2> [hash: {1}]</p></fieldset>";
- string property_start_html = "<table>";
- string property_end_html = "</table>";
- //string property_format_html = @"<li><span class='pn'>{0}</span> :<span class='pv'>{1}</span></li>";
- string property_format_html = @"<tr><td class='pn'>{0}</td><td class='pv'>{1}</td></tr>";
- string null_value_html = "[null]";
- start_format = start_format_html;
- end_format = end_format_html;
- property_start = property_start_html;
- property_end = property_end_html;
- property_format = property_format_html;
- null_value = null_value_html;
- }
- //writer.Indent += 1;
- Type t = obj.GetType();
- var asilist = obj as IEnumerable<object>;
- if(asilist != null) {
- if(asilist.Count() == 0)
- return;
- foreach(var item in asilist) {
- WriteObjectInternal(item,writer,depth,html);
- }
- return;
- }
- if(t.IsArray) {
- ICollection col = obj as ICollection;
- if(col.Count == 0)
- return;
- foreach(var item in col) {
- WriteObjectInternal(item,writer,depth,html);
- }
- return;
- }
- Dictionary<string,object> allProps = GetPropertyValues(obj);
- Dictionary<string,object> complexProps = new Dictionary<string,object>();
- writer.WriteLine(start_format.FormatString(t.Name,obj.GetHashCode()));
- writer.Indent += 1;
- //write simple properties
- if(property_start != null)
- writer.Write(property_start);
- foreach(string propname in allProps.Keys) {
- object propval = allProps[propname];
- if(propval == null) {
- if(html)
- writer.Write(property_format.FormatString(propname,null_value));
- else
- writer.WriteLine(property_format.FormatString(propname,null_value));
- }
- else {
- var propilist = obj as IEnumerable<object>;
- if(propilist != null) {
- if(propilist.Count() > 0) {
- complexProps.Add(propname,propval);
- }
- }
- else if(propval.GetType().IsArray) {
- complexProps.Add(propname,propval);
- }
- if(!propval.GetType().Namespace.StartsWith("System") && !propval.GetType().IsEnum) {
- if(!complexProps.ContainsKey(propname))
- complexProps.Add(propname,propval);
- }
- else {
- if(html)
- writer.Write(property_format.FormatString(propname,propval ?? null_value));
- else
- writer.WriteLine(property_format.FormatString(propname,propval ?? null_value));
- }
- }
- }
- if(property_end != null)
- writer.Write(property_end);
- //write complex properties
- foreach(string propname in complexProps.Keys) {
- object propval = allProps[propname];
- if(propval == null)
- continue;
- writer.Indent += 1;
- try {
- if(propval.GetType().IsArray) {
- ICollection col = propval as ICollection;
- if(col != null && col.Count > 0 && !propval.GetType().IsEnum) {
- string writeval = "" + propname + " : Length = " + (col.Count) + " | Type = " + propval.GetType().FullName + "";
- writer.WriteLine();
- writer.Write("BEGIN PROPERTY ");
- writer.Write("".PadLeft(10,'*'));
- writer.Write(writeval);
- writer.Write("".PadLeft(10,'*'));
- writer.WriteLine();
- WriteObjectInternal(propval,writer,depth,html);
- writer.WriteLine();
- writer.Write("END PROPERTY");
- writer.Write("".PadLeft(10,'*'));
- writer.Write(writeval);
- writer.Write("".PadLeft(10,'*'));
- writer.WriteLine();
- }
- else {
- writer.WriteLine("" + propname + " : ");
- WriteObjectInternal(propval,writer,depth,html);
- }
- }
- else {
- writer.WriteLine("" + propname + " : ");
- WriteObjectInternal(propval,writer,depth,html);
- }
- }
- catch(Exception error) {
- writer.WriteLine(error.Message);
- }
- finally {
- writer.Indent -= 1;
- }
- }
- writer.Indent -= 1;
- writer.WriteLine(end_format.FormatString(t.Name,obj.GetHashCode()));
- writer.Flush();
- }
- static Dictionary<string,object> GetPropertyValues(object obj) {
- var dictionary = TypeDescriptor.GetProperties(obj)
- .OfType<PropertyDescriptor>()
- .OrderBy(p => p.Name)
- .ToDictionary(p => p.Name,p => p.GetValue(obj));
- return dictionary;
- }
- }
- #endregion
- #region String Extensions class
- [System.Diagnostics.DebuggerStepThrough]
- public static class StringExtensions {
- public static string ToString(this string s,params object[] args) {
- return string.Format(s,args);
- }
- public static string FormatWith(this string s,params object[] args) {
- return string.Format(s,args);
- }
- public static string FormatString(this string s,params object[] args) {
- return string.Format(s,args);
- }
- public static bool IsNull(this string s) {
- return (s == null
- || s.Length == 0
- || string.IsNullOrWhiteSpace(s));
- }
- public static string ValueIfNull(this string s,string value) {
- if(IsNull(s))
- return value;
- else
- return s;
- }
- public static string ToStringEmpty(this object obj) {
- return obj == null ? string.Empty : obj.ToString();
- }
- public static string ToStringEmpty(this object obj,string valueIfNull) {
- return obj == null ? valueIfNull : obj.ToString();
- }
- public static string ToTitleString(this string s) {
- if(s == null)
- return null;
- string result = "";
- for(int i = 0;i < s.Length;i++) {
- char c = s[i];
- if(char.IsUpper(c)) {
- if(i != 0) {
- int h = i - 1;
- if(h > -1) {
- char p = s[h];
- if(char.IsUpper(p)) {
- result += c.ToString();
- continue;
- }
- }
- result += " ";
- }
- }
- result += c.ToString();
- }
- return result;
- }
- public static string Repeat(this string s,int num) {
- return Repeat(s,num,string.Empty);
- }
- public static string Repeat(this string s,int num,string delimeter) {
- if(IsNull(s))
- return s;
- StringBuilder sb = new StringBuilder();
- for(int i = 0;i < num;i++) {
- sb.Append(s);
- if(delimeter != null)
- sb.Append(delimeter);
- }
- return sb.ToString();
- }
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment