Search code examples
c#classinner-classes

How to get property value that is a class itself within a class method


I want to create some classes, that have to look a certain way (the way shown in my example).

Some of the class properties are classes (or structs) themselves.

I want to write a method within my classes that get the property values of all the properties that are Structs and write them to a string.

So this is what my classes look like:

public class car
{
   public string brand { get; set; }
   public tire _id { get; set; }
   public string GetAttributes()
   {
      Type type = this.GetType();
      PropertyInfo[] properties = type.GetProperties();
      foreach(PropertyInfo propertyInfo in properties)
      if (propertyInfo.PropertyType.ToString().Contains("_"))
      {
         //I want to write the actual value of the property here!
         string nested_property_value = ...
         return nested_property_value;
      }
   }
}

this is what my structs look like:

 public struct tire
 {
    public int id { get; set; }
 }

this would be the Main Program:

tire mynewtire = new tire()
{
   id = 5
};

car mynewcar = new car()
{
   _id = mynewtire
};

How can I create a working method to get these property attributes?


Solution

  • This code will get you started. I recommend you look at other serialisation methods (such as JSON) as well.

    using System;
    
    namespace Test
    {
        public class car
        {
            public string brand { get; set; }
            public tire _id { get; set; }
    
            public string GetAttributes()
            {
                var type = this.GetType();
                var returnValue = "";
                var properties = type.GetProperties();
                foreach (var propertyInfo in properties)
                {
                    // Look at properties of the car
                    if (propertyInfo.Name.Contains("_") && propertyInfo.PropertyType.IsValueType &&
                        !propertyInfo.PropertyType.IsPrimitive)
                    {
                        var propValue = propertyInfo.GetValue(this);
    
                        var propType = propValue.GetType();
                        var propProperties = propType.GetProperties();
    
                        foreach (var propPropertyInfo in propProperties)
                        {
                            // Now get the properties of tire
                            // Here I just concatenate to a string - you can tweak this
                            returnValue += propPropertyInfo.GetValue(propValue).ToString();
                        }
                    }
                }
                return returnValue;
            }
        }
    
        public struct tire
        {
            public int id { get; set; }
        }
    
        public class Program
        {
            static void Main(string[] args)
            {
                var mynewtire = new tire()
                {
                    id = 5
                };
    
                var mynewcar = new car()
                {
                    _id = mynewtire
                };
                Console.WriteLine(mynewcar.GetAttributes());
    
                Console.ReadLine();
            }
        }
    }