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 :: unity hexmapping 
Csharp :: no cameras rendering unity 
Csharp :: c# different getter setter types 
Csharp :: populate array from an XML file 
Csharp :: c# winform get access token facebook 
Csharp :: how to subtract two rows asp ne gridview in asp.net 
Csharp :: C# HttpUtility not found / missing C# 
Csharp :: c# ilogger for inherited class 
Csharp :: dapper extension 
Csharp :: Xamarin forms XAML change value 
Csharp :: save and query mongodb collection as dynamic ExpandoObject 
Csharp :: how to combine constructors in c# 
Csharp :: // Force WPF to render UI changes immediately with this magic line of code... 
Csharp :: pyqt send message to another instance 
Csharp :: drop column with code first asp.net core 
Csharp :: get datacontext of itemscontrol item c# 
Csharp :: enumerate dictionary c# 
Csharp :: csgo crashes at retrieving game data 
Csharp :: c# registrykey is null 
Csharp :: best unity regex for email validation in c# 
Csharp :: c# ienumerable unassigned 
Csharp :: flutter find a widget 
Csharp :: c# use meditor from service 
Csharp :: my context class is in different project and i want migration in different project in asp.net mvc 
Csharp :: int to binary string with 4 characters 
Csharp :: c# convert timestamp to datetime 
Csharp :: username and password into base64 encoding c# 
Csharp :: array hw exercise 
Csharp :: C# dictionnaries 
Csharp :: BindableDynamicDictionary 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =