Answers for "C# print all properties of an object including children objects"

C#
0

C# print all properties of an object including children objects

public void DisplayObject(object obj)
{
	var type = obj.GetType();
	foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(type))
	{
		if (descriptor.PropertyType.IsClass 
			&& descriptor.PropertyType.Assembly.FullName == type.Assembly.FullName)
		{
			var value = descriptor.GetValue(obj);
			DisplayObject(value);
		}
		else
		{
			string name = descriptor.Name;
			object value=descriptor.GetValue(obj);
			Console.WriteLine("{0}={1}",name,value);
		}
	}
}
Posted by: Guest on June-18-2021

Code answers related to "C# print all properties of an object including children objects"

C# Answers by Framework

Browse Popular Code Answers by Language