Home > C#, LINQ > Functional Type member resolution with C#

Functional Type member resolution with C#

An interesting problem cropped up today regarding reflection and how LINQ might make for a strongly typed solution. K. Scott Allen suggested a while back to use lambdas to remove the use of ‘magic’ strings in building Fetching Intentions used in NFetchSpec and it was suggested by my colleague Mark that a similar approach be used in resolving a types property name using strongly typed expressions. Given the following class:

public class Criteria
{
    private int _pageCounter;

    public int PageCounter
    {
        get { return _pageCounter; }
        set { _pageCounter = value; }
    }

    private string _levels;

    public string Levels
    {
        get { return _levels; }
        set { _levels = value; }
    }
}

I want to dynamically resolve any property name using an Expression such as:

Expression<Func<Criteria, string>> f = c => c.Levels;

After fumbling around for a while it occurred that functional programming was yet again the simplest way to approach this.

public class PropertyNameResolver<T>
{
    public static string Resolve<TPropertyType>
        (Expression<Func<T, TPropertyType>> expression)
    {
        Func<Expression<Func<T, TPropertyType>>, string>
            prop_name_function = l =>
            {
                int posDot = l.Body.ToString().LastIndexOf(“.”) + 1;
                return l.Body.ToString().Remove(0, posDot);
            };
        var property_name =
            typeof(T).GetProperty(prop_name_function(expression)).Name;
        return property_name;
    }
}

By supplying the expression and encapsulating the function that takes the expression as an argument, we get access to the body and parameters in the expression tree built by the original lambda and when be execute the outer function we can extrapolate the property name. Here’s an example of the usage:

var pc = PropertyNameResolver<Criteria>.Resolve<int>(c => c.PageCounter);
Console.WriteLine(pc);

Functional goodness in C#.

Share/Save/Bookmark

Categories: C#, LINQ Tags: ,
  1. Kris
    June 12th, 2009 at 08:43 | #1

    Very nice, I’m not sure where I got this, but I’ve been using a similar extension method on “Object” to dynamically resolve property values:

    public static T GetPropertyValue(this object component, string propertyName)
    {
    var p = TypeDescriptor.GetProperties(component)[propertyName];
    if (p == null && propertyName.Contains(’.'))
    {
    object obj = component;
    foreach (var segment in propertyName.Split(’.'))
    {
    if (obj == null)
    break;
    p = TypeDescriptor.GetProperties(obj)[segment];
    obj = p.GetValue(obj);
    }
    return (T)obj;
    }
    else
    return (T)p.GetValue(component);
    }

    You may be able to come up with some simpler LINQ-style logic to simplify the code.

    [Reply]

  2. Cherian
  1. June 15th, 2009 at 16:44 | #1
  2. June 16th, 2009 at 07:28 | #2

Creative Commons Attribution-ShareAlike 2.5 Australia
Creative Commons Attribution-ShareAlike 2.5 Australia