Search code examples
c#reflectionnestedpropertyinfoderived-types

Using reflection with derived objects


I am new to C# and need to perform a certain task using Reflection.

The thing is like this: I have a class called Derived which derives form a class called Base. Within the Base class I have another public class, which is a property called Prop class. Within the Prop class , there is a public property of type String called propString. Both Derived and Base class are under the same namespace. I depicted the situation below:

namespace mynamespace

public class Base {

    public Prop prop { get ; set;}


}

namespace mynamespace

public class Derived : Base {

    // some other properties of the derived class , not so relevant....
}


public class Prop {

     public String propString {get; set;}
}

I need to write two functions:

The first one receives a string of the "full path" of a property within a class and needs to extract the type of that property (In my case the string will be "Prop.propString" and the outcome of this method needs to be A PropertyInfo object with that property).

The second one gets an instance of an object and needs to perform a manipulation on the propString property (in my case the object that the function will get is A Derived object). I understood that it can be implemented in "more or less" that manner , yet it does not work well at the moment.

public void SecondFunc(Base obj) 
{
         PropertyInfo propertyInfo;
         object obj = new object();
         string value = (string)propertyInfo.GetValue(obj, null);

         string afterRemovalValue = myManipulationStringFunc(value);

         propertyInfo.SetValue(obj, afterRemovalValue, null);

}

Please your advises on how to implement these two functions, and off course any further insights you have will be highly appreciated.

Thanks allot in advance,

Guy.


Solution

  • I am not sure what you are trying to accomplish and if it is the best way to do it. But I have changed the code so it works. I have not made it as dynamic as it can be...

    public class Base
    {
        public Prop prop { get; set; }
    }
    
    public class Derived : Base
    {
    
        // some other properties of the derived class , not so relevant....
    }
    
    public class Prop
    {
    
        public String propString { get; set; }
    }
    public class MyClass
    {
        public void SecondFunc(object obj)
        {
            Type type = obj.GetType();
            var allClassProperties = type.GetProperties();
            foreach (var propertyInfo in allClassProperties)
            {
                if (propertyInfo.PropertyType == typeof(Prop))
                {
                    var pVal = (Prop)propertyInfo.GetValue(obj, null);
                    if(pVal == null)
                    {
                        //creating a new instance as the instance is not created in the ctor by default
                        pVal = new Prop();
                        propertyInfo.SetValue(obj, pVal, null);
                    }
                    this.SecondFunc(pVal);
                }
                else if (propertyInfo.PropertyType == typeof(string))
                {
                    string value = (string)propertyInfo.GetValue(obj, null);
                    string afterRemovalValue = myManipulationStringFunc(value);
    
                    propertyInfo.SetValue(obj, afterRemovalValue, null);
                }
            }
        }
    
        private string myManipulationStringFunc(string value)
        {
            if (string.IsNullOrEmpty(value))
                value = "Value was NULL";
            return value;
        }
    }
    

    I hope this helps...