Reflection not extracting values from: Sealed Class having a property, which is object of another sealed class.

StackOverflow https://stackoverflow.com/questions/11010044

  •  14-06-2021
  •  | 
  •  

Question

I am recrusively evaluating properties via reflection using object.GetType().GetProperty(string propertyName).

This is working fine in case the obj is of sealed class but having property as an object of normal public class. However if this property contains a sealed class object, it doesnt return any property via GetProperty() method.

Trying to iterate recursively from Baseclass object via Prpty1 -->Prpty2-->Prpty3. param.Properties contains string array for Prpty1 ,Prpty2,Prpty3. Am not able to get Prpty2, it is coming as Null. Using the methods written below for this access.

Sample Code is as follows:

//Methods below are used to extract value from Object (basically BaseClass Object)

 private string EvaluateObject(object obj, MappedParameter param)
    {
        object evaluatedObj = obj;
        foreach (string s in param.Properties)
        {
            evaluatedObj = EvalProperty(evaluatedObj, s);
        }
        return (evaluatedObj == obj ? null : evaluatedObj.ToString());
    }

    private object EvalProperty(object obj, string propertyName)
    {
        System.Reflection.PropertyInfo propInfo = obj.GetType().GetProperty(propertyName);
        if (propInfo == null)
            throw new Exception("Invalid Property token");
        object propertyValue = propInfo.GetValue(obj, null);
        return propertyValue;
    }


//Below classes are Data Wrappers

 namespace TryClassLibrary
 {
 public class BaseClass
 {
    private NestedClass _Data = new NestedClass();    

    public NestedClass Prpty1
    {
        set { _Data = value; }
        get { return _Data; }
    }
}
}

 namespace TryClassLibrary
{
public sealed class NestedClass
{
    public int Id { get; set; }

    public NestedNestedClass Prpty2 = new NestedNestedClass();
}

public sealed class NestedNestedClass
{
    private string _Message;

    public NestedNestedClass()
    {
        Prpty3 = "Test value";
    }

    public string Prpty3
    {
        set
        {
            _Message = value;
        }
        get { return _Message; }
    }
}
}

Please can somebody help me finding the way to access Prpty2, Prpty3 or is it some existing constraint in using reflection. Thanks.

Was it helpful?

Solution

Ah, the problem is simple: Prpty2 is not a property: it is a field:

public NestedNestedClass Prpty2 = new NestedNestedClass();

should be:

private readonly NestedNestedClass prpty2 = new NestedNestedClass();
public NestedNestedClass Prpty2 { get { return prpty2; } }

(or similar)

Then it all works:

object obj = new BaseClass();
obj = obj.GetType().GetProperty("Prpty1").GetValue(obj, null);
obj = obj.GetType().GetProperty("Prpty2").GetValue(obj, null);
obj = obj.GetType().GetProperty("Prpty3").GetValue(obj, null);
string s = obj.ToString(); // "Test value"

For info, if you are doing lots of this - maybe look at FastMember; it is more convenient, and heavily optimised so to not pay the reflection tax.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top