3

I have following two classes

public class Family
{
    public string ChildName { get; set; }
}

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Family Child { get; set; }

}

I have an instance of Employee class as follows.

 Employee employee = new Employee();
 employee.Name = "Ram";
 employee.Id = 77;
 employee.Child = new Family() { ChildName = "Lava" };

I have a method which gets the property value based on the property name as follows:

public static object GetPropertyValue(object src, string propName)
{
  string[] nameParts = propName.Split('.');

 if (nameParts.Length == 1)
  {
    return src.GetType().GetRuntimeProperty(propName).GetValue(src, null);
  }

foreach (String part in nameParts)
{
    if (src == null) { return null; }

    Type type = src.GetType();

    PropertyInfo info = type.GetRuntimeProperty(part);

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

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

In the above method,when I try to get property value of nested class like

GetPropertyValue(employee, "employee.Child.ChildName")  

or

GetPropertyValue(GetPropertyValue(employee, "Family"), "ChildName"

doesn't return any value because type.GetRuntimeProperty(part) is always null.

Is there any way to fix this problem?

3 Answers 3

2

You problem lies in this line:

foreach (String part in nameParts)

Because you are iterating over each part of nameParts, you are also iterating over "employee", which of course is not a valid property.

Try either this:

foreach (String part in nameParts.Skip(1))

Or calling the method like this:

GetPropertyValue(employee, "Child.ChildName")

(Notice no "employee.", because you already pass in an employee)

Sign up to request clarification or add additional context in comments.

Comments

1

The problem in this case is that when you split the string employee.Child.ChildName, the "employee" is the first part. However, employee is not a property of the source i.e. Employee Class.

Try this:

public class Program
{
    public static void Main()
    {
        Employee employee = new Employee();
        employee.Name = "Ram";
        employee.Id = 77;
        employee.Child = new Family() { ChildName = "Lava" };
        GetPropertyValue(employee, "employee.Child.ChildName");

    }


    public class Family
    {
        public string ChildName { get; set; }
    }

    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public Family Child { get; set; }

    }

    public static object GetPropertyValue(object src, string propName)
    {
        string[] nameParts = propName.Split('.');

        if (nameParts.Length == 1)
        {
            return src.GetType().GetRuntimeProperty(propName).GetValue(src, null);
        }
        nameParts = nameParts.Skip(1).ToArray();
        foreach (String part in nameParts)
        {
            if (src == null) { return null; }

            Type type = src.GetType();

            PropertyInfo info = type.GetRuntimeProperty(part);

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

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

Here, i have skipped the first part of the string i.e. "employee". However, you can solve the problem by passing Child.ChildName

Comments

-1

This question is around 2 years old, but I found a another working solution for you question, which is easy to understand. If you initialize the object in calling calss constructor you can use dot(.) notation to assign or read property. Example -

public class Family{
    public string ChildName { get; set; }
}

public class Employee{
    public int Id { get; set; }
    public string Name { get; set; }
    public Family Child { get; set; }

    public Employee(){
        Child = new Family();
    }
}

Employee emp = new Employee();
emp.Family.ChildName = "Nested calss attribute value";

1 Comment

You can do that in OPs example as well (no need for a constructor), however, the question was about why his reflection method didn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.