0

Given any object, I want to be able to retrieve the value of a property and any "depth".

var name = myObject.GetValue<String>("Name")

or

var father_name = myObject.GetValue<String>("Father.Name")

This is easy and I'm able do achieve it with the following code:

public static T GetValue<T>(this Object obj, String fqname)
{
    try
    {
        Object value = obj;

        foreach (var prop in fqname.Split('.').Select(s => value.GetType().GetProperty(s)))
        {
            value = prop.GetValue(value, null);
        }

        if (value is T)
        {
            return (T)value;
        }
        else
        {
            // if the type requested is not the same as the stored, attempt a blind conversion
            var converter = TypeDescriptor.GetConverter(typeof(T));
            return (T)converter.ConvertFromInvariantString(value.ToString());
        }
    }
    catch (NotSupportedException)
    {
        throw new InvalidCastException($"Cannot convert value to reuested type");
    }
}

Now the problem is that this doesn't work for arrays, like:

var main_street = myObject.GetValue<String>("Addresses[0].StreetName")

Not even for the cases of arrays of arrays and so on...

I can start adding these conditions and special cases to my code but before that I figured, as C# already does this, maybe we could leverage some code parsing strategy, Roslyn, ... don't know, something that doesn't feel like reinventing the wheel and supports as many cases as possible.

Any ideas?

AlexCode
  • 4,055
  • 4
  • 33
  • 46
  • May I ask why you need to do this in the first place? I honestly try to avoid any solution that requires reflection if possible. – juharr Mar 18 '18 at 19:39
  • What is the minimum .NET version needed? – Timo Mar 18 '18 at 19:40
  • No minimum version. I'm on 4.6.* but I guess 4.7.* is also fine. – AlexCode Mar 18 '18 at 19:44
  • Why? It's for a DSL I'm doing and this is to be used in one of the methods to get a velue from a property bag, as a string (to support a text base configuration). – AlexCode Mar 18 '18 at 19:46
  • This sounds like an extremely slow and inefficient way of implementing [`Dictionary`](https://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx)s to me. “It's for a DSL” would suggest an [Abstract Syntax Tree](https://stackoverflow.com/questions/10678242/), not reflection, to me; but you haven't shown us enough of your “DSL” to make a recommendation. It would help if you explained what your end goal is, not [how you have decided to implement your goal](https://meta.stackexchange.com/questions/66377/). – Dour High Arch Mar 18 '18 at 20:11
  • Performance is not an issue here. The usage of this is very esporadic and for very specific cases. AST is also an option, that's why I mentioned Roslyn. I A Dictionary is exactly what I have but the value can be an object. In those cases, I want to be able to fetch an "inner" propery withouth actually having a type. Consider that it can be a Dynamic type or a JToken as a result of a JSON deserialization. I'll probably have a look at Roslyn Scripting. – AlexCode Mar 18 '18 at 21:07

0 Answers0