Search code examples
c#classgenericsxml-deserialization

How do I get all subclass properties based on name


I have the following structure:

 public class A
 {
    public A1Type A1 { get; set; }
    public A2Type A2 { get; set; }
    public A3Type A3 { get; set; }
  }

  public A1Type
  {
    public string B1 { get; set; }
    public string B2 { get; set; }
  }

And similar with A1Type is also A2Type and A3Type. I have a more complex structure.

I receive a string parameter A1Type and I need to get from A just the subclass A1Type with its values.

Can somebody help me with this?


Solution

  • This might do what you want:

    class Program
    {
        static void Main(string[] args)
        {
            var a = new A()
            {
                A1 = new A1Type() { B1 = "A1Type B1 prop value", B2 = "A1Type B2 prop value" },
                A2 = new A2Type() { B1 = "A2type B1 prop value", B2 = "A2Type B2 prop value" },
                A3 = new A3Type() { B1 = "A3Type B1 prop value", B2 = "A3Type B2 prop value" }
            };
            ListPropertyValuesOf("A1Type", a);
            ListPropertyValuesOf("A2Type", a);
            ListPropertyValuesOf("A3Type", a);
        }
    
        static void ListPropertyValuesOf(string propName, A classA)
        {
            // get all properties of classA object
            var props = classA.GetType().GetProperties();
            foreach (var prop in props)
            {
                if (prop.PropertyType.Name == propName)
                {
                    // get the specific sub-type object on classA
                    var subClassA = classA.GetType().GetProperty(prop.Name).GetValue(classA);
    
                    // loop each of the properties on subClassA and write its value
                    foreach (var subProp in subClassA.GetType().GetProperties())
                        Console.WriteLine(subProp.GetValue(subClassA));
                }
            }
        }
    }
    
    public class A
    {
        public A1Type A1 { get; set; }
        public A2Type A2 { get; set; }
        public A3Type A3 { get; set; }
    }
    public class A1Type
    {
        public string B1 { get; set; }
        public string B2 { get; set; }
    }
    
    public class A2Type
    {
        public string B1 { get; set; }
        public string B2 { get; set; }
    }
    
    public class A3Type
    {
        public string B1 { get; set; }
        public string B2 { get; set; }
    }