0

I am using reflection to instantiate a class and insert values in its nested properties.

Here's an example of what im trying to do:

https://dotnetfiddle.net/pBgeeV

private static void ApplyValueToProperty(object entityInstance, Type instanceType, string propName, string propValue)
{
    // Current only supports an object inside another object.
    // Future TODO> GO RECURSIVE TO FIND AND INSTANTIATE ALL NESTED PROPERTIES OR CLASSES!
    if (propName.Contains("."))
    {
        string[] splittedName = propName.Split('.');
        // Get type of current property
        PropertyInfo currentProp = instanceType.GetProperty(splittedName[0]);
        Type nestedPropertyType = currentProp.PropertyType;
        // Get current object applied to this current prop
        var otherObject = currentProp.GetValue(entityInstance);
        if (otherObject == null)
        {
            // Create instance of nested property class and set its value to a new instance
            otherObject = Activator.CreateInstance(nestedPropertyType);
            currentProp.SetValue(entityInstance, otherObject);
        }
        
        SetSafePropertyValue(otherObject, nestedPropertyType, splittedName[1], propValue);
    }
    else
    {
        SetSafePropertyValue(entityInstance, instanceType, propName, propValue);
    }
}
  1. First I create the main class outside of this method scope. (Test class).
  2. Then all other propName will have the value of nested properties or classes, example:

I will have a name like "OtherTest.AnotherTest.Name",

  • If 'Test' has another property of type 'OtherTest', it would create 'OtherTest' and then create 'AnotherTest' and then put value on correct 'Name'.

I would like to change the first if statement to be recursive and instantiate de correct class and insert values to the correct property. In the end I want to have a complete object created with my necessary properties instantiated and values applied.

Note: I want to avoid using GetProperties() because my class has lots of properties from base members, and it consumes a lot of memory.

andrecj
  • 147
  • 1
  • 13
  • 1
    Seems like you are trying to implement Deep Clone have you considered using something that is described there https://stackoverflow.com/questions/129389/how-do-you-do-a-deep-copy-of-an-object-in-net, https://stackoverflow.com/questions/78536/deep-cloning-objects – Rand Random Mar 08 '21 at 12:53
  • But to create a clone I would have to firstly have an object. I have nothing, only strings with properties names and values. But didnt know about DeepClone and MemberWise clone seemed like a good solution but not for this case I think. – andrecj Mar 08 '21 at 13:12
  • 1
    Instead of this line `SetSafePropertyValue(otherObject, nestedPropertyType, splittedName[1], propValue);` you could do this `ApplyValueToProperty(otherObject, otherObject.GetType(), string.Join(".", splittedName.Skip(1)), propValue);` does this help? – Rand Random Mar 08 '21 at 13:37
  • Well, didnt thought about that. I really need to improve on recursion! I think it will help a lot. Need to look for more edges cases in order to stop recursion. But in fact it does the job very quickly! Thank you so much! – andrecj Mar 08 '21 at 14:20
  • No problem, you are welcome. – Rand Random Mar 08 '21 at 14:21

1 Answers1

1

Instead of this line

SetSafePropertyValue(otherObject, nestedPropertyType, splittedName[1], propValue); 

you could do this

ApplyValueToProperty(otherObject, otherObject.GetType(), string.Join(".", splittedName.Skip(1)), propValue);

The idea is to recursivly call ApplyValueToProperty with the first part of the propName dropped hence the Skip(1) in string.Join(".", splittedName.Skip(1)), this will drop the first part again and again till we hit the following condition with false

if (propName.Contains("."))

The else part of this if will stop the recursion.

Rand Random
  • 7,300
  • 10
  • 40
  • 88