-2

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
};

Anyone has an idea how to create the GetAttributes-Methode? I've been trying to figure this out for ages now, but don't get there...

FlixFix
  • 63
  • 9
  • 2
    None of your types have any attributes or any properties. – Servy Jun 29 '17 at 13:26
  • is this a job interview question? – The Bearded Llama Jun 29 '17 at 13:30
  • Have you considered JSON serialisation to convert it to a string? https://stackoverflow.com/questions/15843446/c-sharp-to-json-serialization-using-json-net – mjwills Jun 29 '17 at 14:11
  • No it's not a job interview question. I've only just started wit c-sharp so all this is new to me. So if I have the prefix public it is not considered a property? Or how come _id, brand etc. are no properties? So is there any chance to get the **value** of my _id within the GetAttributes-Methode? – FlixFix Jun 29 '17 at 14:42
  • 1
    What you have declared are _fields_ not properties. To define a property, the syntax is `public int MyProperty { get; set; }`. If you want to use fields you can use `FieldInfo[] fields = type.GetFields();` – Chris Dunaway Jun 29 '17 at 15:05
  • Thanks Chris for your answer, but as I said I only forgot to add the {get; set;} in my question. I've edited the post now. Despite I have tried the GetFields() too, but nothing so far is giving me the actual value. – FlixFix Jun 29 '17 at 15:46

1 Answers1

0

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();
        }
    }
}
mjwills
  • 23,389
  • 6
  • 40
  • 63
  • cheers mate, that solved my problem! really appreciate you taking your time to help me out! As I said I've only just started coding two months ago so I'm not aware of all the possibilities csharp has to offer yet! I'll also look into the other method you suggested. – FlixFix Jun 30 '17 at 06:44