Property as an Argument

This content is licensed under The MIT License. See here for more details.

Regarding using a class property as a method argument. Based on this answer by MatthiasG .

using System;
using System.Linq.Expressions;
using System.Reflection;

void DoSomething<T>(Expression<Func<T>> property)
{
    // Get info
    PropertyInfo propertyInfo = ((MemberExpression)property.Body).Member as PropertyInfo;

    // Get name
    string name = propertyInfo.Name;

    // Get value
    T pValue = property.Compile()();

    // Do something with the variables we've filled.
}

Used like

DoSomething(() => AProperty);