0

I know that I can use C# reflection to find a property using a string (e.g. "Property1") of an object.

What I need to do is generate the entire call using a string. e.g. "Object1.Object2.Property".

How can I do this in C#?

If I can't use reflection for this, what can I use?

FYI I am using this in ASP.NET to access model properties using the name of the form field that binds to that property in the model. If anyone knows another way around this, please suggest it.

Thanks

Armada
  • 718
  • 8
  • 19
  • 1
    Split the string on the dot, use recursion, but it sounds like you could use a [custom model binder](http://www.codeproject.com/Articles/605595/ASP-NET-MVC-Custom-Model-Binder). – CodeCaster Dec 04 '13 at 11:31
  • jheddings answer on This link does what you're describing: http://stackoverflow.com/questions/1196991/get-property-value-from-string-using-reflection-in-c-sharp – Alexandre Machado Dec 04 '13 at 11:35
  • @CodeCaster I was thinking about this. Would this also work with "Object.Property[1]", for example? – Armada Dec 04 '13 at 11:38
  • It will if you parse the `[n]` to an `index` parameter of `Property.GetValue()`. – CodeCaster Dec 04 '13 at 11:41

2 Answers2

0

Include theses namespaces:

using System.Reflection;
using System.Linq;

and try something like this:

public string ReadProperty(object object1)
{
    var object2Property = object1.GetType().GetProperties().FirstOrDefault(x => x.Name == "Object2");
    if (object2Property != null)
    {
        var anyProperty = object2Property.GetType().GetProperties().FirstOrDefault(x => x.Name == "Property");
        if (anyProperty != null)
        {
            var object2Value = object2Property.GetValue(object1, null);

            if (object2Value != null)
            {
                var valueProperty = anyProperty.GetValue(object2Value, null);

                return valueProperty;
            }
        }
    }

    return null;
}

Just replace the names of properties for your correct proeprties.

Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
0

Here is a working code to get property value with specified string:

static object GetPropertyValue(object obj, string propertyPath)
{
    System.Reflection.PropertyInfo result = null;
    string[] pathSteps = propertyPath.Split('/');
    object currentObj = obj;
    for (int i = 0; i < pathSteps.Length; ++i)
    {
        Type currentType = currentObj.GetType();
        string currentPathStep = pathSteps[i];
        var currentPathStepMatches = Regex.Match(currentPathStep, @"(\w+)(?:\[(\d+)\])?");
        result = currentType.GetProperty(currentPathStepMatches.Groups[1].Value);
        if (result.PropertyType.IsArray)
        {
            int index = int.Parse(currentPathStepMatches.Groups[2].Value);
            currentObj = (result.GetValue(currentObj) as Array).GetValue(index);
        }
        else
        {
            currentObj = result.GetValue(currentObj);
        }

    }
    return currentObj;
}

And then you can get values queries, including arrays, for example:

var v = GetPropertyValue(someClass, "ArrayField1[5]/SomeField");
Konrad Kokosa
  • 16,563
  • 2
  • 36
  • 58