Language: C#
Helper function for creating a dynamic to string based on properties of an object.
public class StringHelper { public static string ToString(object instance) { var properties = TypeDescriptor.GetProperties(instance); StringBuilder output = new StringBuilder(); foreach (PropertyDescriptor property in properties) { output.Append(string.Format("{0} - {1}\r\n", property.Name, property.GetValue(instance))); } return output.ToString(); } }
by Denis Vuyka
October 06, 2009 @ 2:05am
Nice snippet!
If you are dealing with Dependency Objects than TypeDescriptor.GetProperties call against live instance may help. It will give you an opportunity fetching Attached properties as well (because they are assigned for DP only when it is instantiated).
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search


Updated to use StringBuilder and TypeDescriptor - thanks to Denis Vuyka and Krzysztof Kozmic