112  
csharp
 
RU EN
Mar 13

How to get a list of the properties for the object in C#

How to get a list of the properties for the object in C#
Автор:
Sergey Drozdov
Просмотров:
191
How to get a list of the properties for the object in C# favorites 0

During development, sometimes you need to know the state of an object and what data it contains. Debug is not always suitable approach, sometimes it is more convenient to view the state of an object on a web page or there may be a need to save information, for example, to a file.

There are many solutions to implement this task, and one of the simple and effective is to write an Extension:

public static class Extensions
{
    public static string ObjectProperties(this object obj)
    {
        var properties = obj.GetType().GetProperties();
        var dump = new StringBuilder();
        foreach (var prop in properties)
        {
            dump.AppendLine($"{prop.Name}: {prop.GetValue(obj, null)}");
        }

        return dump.ToString();
    }
}

The option for obtaining a list of properties:

public static class Extensions
{
    public static IList<string> ObjectPropertiesList(this object obj)
    {
        var properties = obj.GetType().GetProperties();

        return properties.Select(prop => $"{prop.Name}: {prop.GetValue(obj, null)}").ToList();
    }
}

This is a working solution, but it does not show the properties of nested objects. In this task, there is no need to show data at multiple nesting levels, it is enough to know about the first level.

public static IEnumerable<string> ObjectPropertiesFull(this object obj)
{
    var dump = new List<string>();
    var properties = obj.GetType().GetProperties();
    foreach (var prop in properties)
    {
        if (prop.GetValue(obj, null) is ICollection collectionItems)
        {
            dump.Add(prop.Name);
            foreach (var item in collectionItems)
            {
                dump.Add($" - {item}");
                var objectType = item.GetType();
                foreach (var nestedObjectProp in objectType.GetProperties())
                {
                    dump.Add($" --- {nestedObjectProp.Name}: {nestedObjectProp.GetValue(item)}");
                }
            }
        }
        else if (prop.GetValue(obj, null) is Object nestedObject)
        {
            dump.Add($"{prop.Name}: {prop.GetValue(obj, null)}");
            var nestedType = nestedObject.GetType();
            foreach (var nestedTypeProp in nestedType.GetProperties())
            {
                dump.Add($" --- {nestedTypeProp.Name}: {nestedTypeProp.GetValue(nestedObject)}");
            }
        }
    }

    return dump;
}

This is sufficient in most cases. If there is a need to find out more information about an object, you can use recursion and a deeper analysis of the properties.

Codebase: https://codebase.blackball.lv/project/code-buns/source?file=Extensions/ObjectExtensions.cs#code-buns

Object properties

Файлы

Похожее
Aug 15, 2021
.NET has a large number of built in exceptions. However, there maybe times when none of the built exceptions seem adequate for your particular scenario and you will need to create your own custom (AKA “user defined”) exception. This post...
Oct 20, 2022
Author: Ricardo Griffith
Schedule and run background jobs using this powerful framework Disclaimer: the image mentioned in this article is currently marked as preview. Opinions expressed therein are solely my own and do not express the views or opinions of my employer. Hangfire...
Jan 28, 2023
Author: Rokas
Prerequisites: VSCode and .Net 7 installed. Don't need to copy the code, GitHub link provided in the end. Open up a terminal in VSCode or any IDE of your choice, run: dotnet new console 2 files will appear, default Program.cs...
Oct 14, 2024
Author: Anton Martyniuk
In this article you will learn how to map objects in .NET using various techniques and libraries. We’ll explore what is the best way to map objects in .NET in 2024. What is object mapping What is object mapping and...
Написать сообщение
Тип
Почта
Имя
*Сообщение