0

caI have seen several examples of reflection on here but cant seem to get them to work for my exact issue...

example of a solution I looked at

My 3 classes...

 public class Row
    {
        public string Cell1  = "cat";//{ get; set; }
        public string Cell2 = "fish"; //{ get; set; }
        public string Cell3 = "dog";  //{ get; set; }


    }

    public class Grid
    {
        public Row Row1 = new Row();
        public Row Row2 = new Row();
        public Row Row3 = new Row();

    }

    public class SetOfGrids
    {
        public Grid g1 = new Grid();
        public Grid g2 = new Grid();
    }

How I can get the value...

SetOfGrids sog = new SetOfGrids();
 MessageBox.Show(sog.g1.Row1.Cell1);

The getproperty function for the link i thought might work...

public Object GetPropValue(String name, Object obj) {
    foreach (String part in name.Split('.')) {
        if (obj == null) { return null; }

        Type type = obj.GetType();
        PropertyInfo info = type.GetProperty(part);
        if (info == null) { return null; }

        obj = info.GetValue(obj, null);
    }
    return obj;
}

How I would like to get the value... (I get a null exception error.)

 string PathToProperty = "sog.g1.Row1.Cell1";
 string s = GetPropValue(PathToProperty, sog).ToString;
 MessageBox.Show(s);

Result should be "cat2"

Update changes the Row code to the following as suggested.

public class Row
    {
        public string Cell1  { get; set; }
        public string Cell2 { get; set; }
        public string Cell3 { get; set; }
       public  Row()
        {
            this.Cell1 = "cat";
            this.Cell2 = "dog";
            this.Cell3 = "fish";
        }

    }
Community
  • 1
  • 1
user3755946
  • 804
  • 1
  • 10
  • 22

2 Answers2

2

Problem is you are asking method GetProperty to get you fields, not properties. Try changing the fields to properties or use GetField method.

Divisadero
  • 895
  • 5
  • 18
2

This:

using System;
using System.Linq;
using System.Net;
using System.Reflection;

namespace Test
{
    public class Row
    {
        public string Cell1 { get; set; } 
        public string Cell2 { get; set; } 
        public string Cell3 { get; set; }  
    }

    public class Grid
    {
        public Row Row1 { get; set; } 
        public Row Row2 { get; set; } 
        public Row Row3 { get; set; } 
    }

    public class SetOfGrids
    {
        public Grid g1 { get; set; } 
        public Grid g2 { get; set; } 
    }

    class Program
    {
        public static object GetPropValue(string name, object obj)
        {
            foreach (string part in name.Split('.'))
            {
                if (obj == null) { return null; }

                var type = obj.GetType();
                var info = type.GetProperty(part);

                if (info == null) { return null; }

                obj = info.GetValue(obj, null);
            }

            return obj;
        }

        public static void Main()
        {
            SetOfGrids sog = new SetOfGrids
            {
                g1 = new Grid { Row1 = new Row { Cell1 = "cat", Cell2 = "fish", Cell3 = "dog" }, Row2 = new Row { Cell1 = "cat", Cell2 = "fish", Cell3 = "dog" }, Row3 = new Row { Cell1 = "cat", Cell2 = "fish", Cell3 = "dog" } },
                g2 = new Grid { Row1 = new Row { Cell1 = "cat", Cell2 = "fish", Cell3 = "dog" }, Row2 = new Row { Cell1 = "cat", Cell2 = "fish", Cell3 = "dog" }, Row3 = new Row { Cell1 = "cat", Cell2 = "fish", Cell3 = "dog" } }
            };

            string PathToProperty = "g1.Row1.Cell1";

            object s = GetPropValue(PathToProperty, sog);

            Console.WriteLine(s);
            Console.ReadLine();
        }
    }
}

Works fine. Please note that I changed all fields to actual properties and removed the first part of your string because "sog." is the variable name, not a property.

nvoigt
  • 75,013
  • 26
  • 93
  • 142