---
title: "How can I pass a property in via a lambda expression?"  
description: "How can I pass a property in via a lambda expression?"  
author: "Anonymous User"  
published: 2014-02-04  
updated: 2014-02-04  
canonical: https://www.mindstick.com/forum/1955/how-can-i-pass-a-property-in-via-a-lambda-expression  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# How can I pass a property in via a lambda expression?

There are some [unit tests](https://www.mindstick.com/forum/12675/how-to-do-unit-tests-on-mvc-validation) on my [project](https://www.mindstick.com/articles/105927/how-to-excel-at-managing-multiple-projects) where we want to be able to set some [properties](https://www.mindstick.com/articles/23331/nootropics-7-different-types-and-their-unique-properties) that have [private](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers) setters. Currently I'm doing it via [reflection](https://www.mindstick.com/articles/13004/how-to-write-a-reflection-paper) and this [extension method](https://www.mindstick.com/articles/22/extension-method):

```
public static void SetPrivateProperty(this object sourceObject, string propertyName, object propertyValue){    sourceObject.GetType().GetProperty(propertyName).SetValue(sourceObject, propertyValue, null);}Assuming I had a TestObject like this:public class TestObject{    public int TestProperty{ get; private set; }}
```

I can then call this in my unit tests as follows:

myTestObject.SetPrivateProperty("TestProperty", 1);

However, I'd like to have [validation](https://www.mindstick.com/articles/12234/validation-using-data-annotation-using-entity-framework) of the [property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) name at [compile](https://www.mindstick.com/forum/2316/how-to-views-compile-in-mvc) time, and thus I'd like to be able to pass the property in via [expression](https://www.mindstick.com/articles/1861/sqlite-expressions), like this:

myTestObject.SetPrivateProperty(o => o.TestProperty, 1);

How can I do this?

## Replies

### Reply by Pravesh Singh

Hi John,\

If the getter is public, then the following should work. It will give you an extension method that looks like this:

var propertyName = myTestObject.NameOf(o => o.TestProperty);

It requires a public getter. I hope, at some point, reflection functionality like this is rolled into the language.

```
public static class Name{    public static string Of(LambdaExpression selector)    {        if (selector == null) throw new ArgumentNullException("selector");        var mexp = selector.Body as MemberExpression;        if (mexp ==null)        {            var uexp =(selector.Body as UnaryExpression);            if (uexp== null)                throw new TargetException(                   
"Cannot determine the name of a member using an expression because
the expression provided cannot be converted to a '" +                  
typeof(UnaryExpression).Name + "'."                );            mexp =uexp.Operand as MemberExpression;        }        if (mexp ==null) throw new TargetException(           
"Cannot determine the name of a member using an expression because
the expression provided cannot be converted to a '" +           
typeof(MemberExpression).Name + "'."        );        return mexp.Member.Name;    }    public static string Of<TSource>(Expression<Func<TSource, object>>
selector)    {        return Of<TSource, object>(selector);    }    public static string Of<TSource, TResult>(Expression<Func<TSource,
TResult>> selector)    {        return Of(selector as LambdaExpression);    }}public static class NameExtensions{    public static string NameOf<TSource, TResult>(this TSource obj,
Expression<Func<TSource, TResult>> selector)    {        return Name.Of(selector);    }}
```


---

Original Source: https://www.mindstick.com/forum/1955/how-can-i-pass-a-property-in-via-a-lambda-expression

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
