Passing a property to a method in C#

Nov 14, 2014 • val antonini

A while ago I needed a way to pass a property (i.e. not its value but the actual property) to a method. I can't remember the reason why but I thought it was great how C# allowed this with reflection and lambda expressions. I couldn't remember the context so in the below example is used to get description attribute off a property. This is probably the worst way to get a description off a property, its just to illustrate passing a property.

The method that accepts a property looks like below:

protected static PropertyInfo GetPropertyInfo<T1, T2>(Expression<Func<T1>> expression, T2 parentClass)
{
    var parentClassType = typeof(T2);

    var propertyName = GetPropertyNameFromExpression(expression);
    var propertyInfo = parentClassType.GetProperty(propertyName);
    var propertyValue = propertyInfo.GetValue(parentClass, null);
    //this method just returns the property info but you could get or set the value or do something else of actual value
    return propertyInfo;
}

And you can pass a property to it with the following:

var propertyInfo = GetPropertyInfo(() => aClass.Prop1, aClass);//passing a property as a param

A sample program that grabs the description off a property would look like this:

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

namespace PropertyPassing
{
    public class AClass
    {
        [Description("My Description")]
        public string Prop1 { get; set; }
        public string Prop2 { get; set; }
    }

    class Program
    {
        static void Main()
        {
            var aClass = new AClass
            {
                Prop1 = "Prop Ones Value"
            };

            var propertyInfo = GetPropertyInfo(() => aClass.Prop1, aClass);//passing a property as a param
            var description = GetDescriptionFromPropertyInfo(propertyInfo);

            Console.WriteLine("Description: {0}", description);
            Console.ReadKey();
        }

        protected static PropertyInfo GetPropertyInfo<T1, T2>(Expression<Func<T1>> expression, T2 parentClass)
        {
            var parentClassType = typeof(T2);

            var propertyName = GetPropertyNameFromExpression(expression);
            var propertyInfo = parentClassType.GetProperty(propertyName);
            var propertyValue = propertyInfo.GetValue(parentClass, null);//this method just returns the property info but you could get or set the value or do something else of actual value
            return propertyInfo;
        }

        private static string GetPropertyNameFromExpression<T>(Expression<Func<T>> expression)
        {
            var body = (MemberExpression)expression.Body;
            return body.Member.Name;
        }

        private static string GetDescriptionFromPropertyInfo(PropertyInfo propertyInfo)
        {
            var customAttributes = propertyInfo.GetCustomAttributes(typeof(DescriptionAttribute), true);
            return customAttributes.Any()
                ? customAttributes.Cast<DescriptionAttribute>().First().Description
                : String.Empty;
        }

    }
}

At the moment the GetPropertyInfo<T1, T>(Expression<Func<T1>> expression, T2 parentClass) method requires the parent class to be passed in but there may be a way to get it from the expression parameter.