Daniel Acevedo
Daniel Acevedo

Reputation: 115

How to get a value of a nested class

I'm working on the following problem:

    public class Unity
    {
        public Article Article {get;set;}
        public string Text{get;set;}
        public string Description{get;set;}
        public int NumberOfPages{get;set;}
    }
enter code here
    public class Article
    {
        public int Id{get;set;}
        public string Name{get;set;}
        public Category SubCategory {get;set;}
        public Model Model {get;set}

        public string override ToString(){
             return Name;
        }
    }
    public class Category
    {
        public int Id{get;set;}
        public string Name{get;set;}
        public Category CategoryFather { get;set;}
    }
    public class Model{
        public int Id {get;set;}
        public string Name{get;set;}
        public Model ModelFather{get;set;}
    }

Imagine i have this domain, i will like to copy the Name of the Article which i do, but i need out of the Article the Name of the Model and the Name of the Category.

I have the next code to copy:

public static List<DataPropertyReport> GetPrimitiveProperties<T>(T entity)
        {
            var properties = entity.GetType().GetProperties();

            List<DataPropertyReport> info = new List<DataPropertyReport>();

            foreach (var property in properties)
            {
                Object value = property.GetValue(entity, null);

                    var name = property.Name;
                    info.Add(new DataPropertyReport(name, value!=null?value.ToString():"", 1));    
            }

            return info;
        }

Upvotes: 0

Views: 1312

Answers (1)

user1672994
user1672994

Reputation: 10849

As mentioned by vendettamit, you can achieve using recursion. The below code achieve basic implementation for primitives data type and class. You have to handle various code flow based on your object structure.

    public static List<DataPropertyReport> GetPrimitiveProperties<T>(T entity, string heirarchyName = null)
    {
        List<DataPropertyReport> info = new List<DataPropertyReport>();
        if (entity != null)
        {
            var properties = entity.GetType().GetProperties();

            foreach (var property in properties)
            {
                Object value = property.GetValue(entity, null);

                var name = property.Name;
                var relatedHeirarchyName = string.IsNullOrEmpty(heirarchyName) ? name : string.Concat(heirarchyName, ".", name);
                if (property.PropertyType != typeof(string) && property.PropertyType.IsClass)
                {
                    var reports = GetPrimitiveProperties(value, relatedHeirarchyName);
                    info.AddRange(reports);
                }
                else
                {
                    info.Add(new DataPropertyReport(relatedHeirarchyName, value != null ? value.ToString() : "", 1));
                }
            }
        }

        return info;
    }

For below object

        Unity unity = new Unity()
        {
            NumberOfPages = 1,
            Description = "Test Des",
            Text = "Test Text",
            Article = new Article()
            {
                Id = 1,
                Name = "test Article",
                Model = new Model()
                {
                    Name = "Test Model",
                    Id = 2,
                    ModelFather = new Model()
                    {
                        Id = 3,
                        Name = "Test Father Model"
                    }
                },
                SubCategory = new Category()
                {
                    Name = "Test Category",
                    Id = 4,
                    CategoryFather = new Category()
                    {
                        Id = 5,
                         Name = "Test Category Fathere"
                    }
                }
            }
        };

The output is

Name : Article.Id Value : 1

Name : Article.Name Value : test Article

Name : Article.SubCategory.Id Value : 4

Name : Article.SubCategory.Name Value : Test Category

Name : Article.SubCategory.CategoryFather.Id Value : 5

Name : Article.SubCategory.CategoryFather.Name Value : Test Category Fathere

Name : Article.Model.Id Value : 2

Name : Article.Model.Name Value : Test Model

Name : Article.Model.ModelFather.Id Value : 3

Name : Article.Model.ModelFather.Name Value : Test Father Model

Name : Text Value : Test Text

Name : Description Value : Test Des

Name : NumberOfPages Value : 1

Based on below ToString implementation of DataPropertyReport

    public override string ToString()
    {
        return $"Name : {this.Name}   Value : {this.Value}";
    }

Upvotes: 3

Related Questions