Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

get all properties of an object including children c#

private void DisplayObject(object obj)
{
    var type = obj.GetType();
    foreach(var propertyInfo in type.GetProperties())
    {
        object value = propertyInfo.GetValue(obj, null);
        if(propertyInfo.PropertyType.IsGenericType && 
            propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
        {
            foreach(object o in (IEnumerable)value)
            {
                DisplayObject(o);
            }
        }
        else
        {
            Console.WriteLine(value);
        }
    }
}
Comment

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);
		}
	}
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to exit winforms application and shutdown pc in c# 
Csharp :: how to get relative path in c# 
Csharp :: unity collision.impulse 
Csharp :: c# convert string to datetime dd-mm-yyyy hh-mm-ss 
Csharp :: connect to a database and loop over a recordset in C# 
Csharp :: c sharp system pause equivelent 
Csharp :: multi case in c# 
Csharp :: convert string into float C# 
Csharp :: print hello world in unity 
Csharp :: c# .net core entity framework one to many 
Csharp :: convert list to datatable c# 
Csharp :: bind repeater to dictionary 
Csharp :: get current location latitude and longitude in xamarin - NAYCode.com 
Csharp :: convert video to byte array c# 
Csharp :: raq query ef core 
Csharp :: Response.Redirect cannot be called in a Page callback 
Csharp :: unity error log 
Csharp :: enum in combobox wpf 
Csharp :: play sound in sequence unity 
Csharp :: c# move directory 
Csharp :: c# anonymous type as return value 
Csharp :: c# datediff 
Csharp :: c# convert bitmap to image 
Csharp :: crud operation in asp.net 
Csharp :: DateTime restrictions 
Csharp :: orderby c# 
Csharp :: c# sequential struct with fixed array size 
Csharp :: C# ToCsv Extension Method 
Csharp :: iserviceprovider vs iservicecollection 
Csharp :: c# on alt + f4 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =