Поиск  
Always will be ready notify the world about expectations as easy as possible: job change page
вчера

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
Просмотров:
49
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

Файлы

Похожее
Apr 11, 2023
Nowadays, everybody so concerned about DDD and how to implement business logic properly that people just forget about existence of other layers. Bada-bing bada-boom, other layers do exist. Shocked, don’t ya? Take your sit, you will be even more shocked...
May 13, 2023
Author: Juan Alberto España Garcia
Introduction to Async and Await in C# Asynchronous programming has come a long way in C#. Prior to the introduction of async and await, developers had to rely on callbacks, events and other techniques like the BeginXXX/EndXXX pattern or BackgroundWorker....
Jan 9, 2024
Author: Juan España
Content index Understanding HttpClientHandler and its importance in C# What is HttpClientHandler in C# Significance of HttpClientHandler in .NET applications Delve deeper into HttpClient C# How to use HttpClient C# C# HttpClient example for beginners Role of System.Net.Http.HttpClient in network...
Aug 9, 2021
Author: MBARK T3STO
If you’re anything like me, you’ve looked for ways of making your applications faster. In this article, I’ll show you a somewhat unknown class in the .NET framework that makes lazy creation of objects easy to do and thread safe....
Написать сообщение
Тип
Почта
Имя
*Сообщение
RSS