Get the parameter name and value from a lamba expression

I’ve recently been working on a design-by-contract library for a new project and thought it would be worth blogging some of the cooler methods as they might be useful.

In this blog I’m going to show an implementation of a Require IsNotNull method which internally gets the parameter name and value from a passed in lambda expression and uses this information to throw an exception with a useful error message if the parameter is null.

The Require IsNotNull method

The Require IsNotNull method is use to check a pre-condition before running a method.

For example, if your method relies on a parameter not being null, the method would throw an exception if a null is passed in.

Here is an example usage of the method:

  1.         public void MyMethod(string myParameter)
  2.         {
  3.             Require.IsNotNull(() => myParameter);
  4.  
  5.             // method implementation ommited.
  6.         }

As you can see, the parameter is passed in as an expression: () => myParameter.

The exception message thrown could be something like: the parameter named ‘myParameter’ cannot be null.

 

How it works.

Firstly we need to get the parameter name so we can build up a useful exception message.

This is done through the use of expressions.

  1.         public static string GetParameterName<TParameter>(Expression<Func<TParameter>> parameterToCheck)
  2.         {
  3.             MemberExpression memberExpression = parameterToCheck.Body as MemberExpression;
  4.  
  5.             string parameterName = memberExpression.Member.Name;
  6.  
  7.             return parameterName;
  8.         }

As you can see, the body of the parameter expression is cast and evaluated as a member expression, then the name of the parameter is easily retrieved.

The next task is to get the value of the parameter so we can ascertain whether it is null.

  1.         public static TParameter GetParameterValue<TParameter>(Expression<Func<TParameter>> parameterToCheck)
  2.         {
  3.             TParameter parameterValue = (TParameter)parameterToCheck.Compile().Invoke();
  4.  
  5.             return parameterValue;
  6.         }

As you can see, the parameter expression is compiled and then invoked and the resulting value is returned.

 

Putting it all together

Ok, the following is quite a crude example but it should give a fairly good idea of how useful this can be:

  1.         public static void IsNotNull<TParameter>(Expression<Func<TParameter>> parameter)
  2.         {
  3.             string parameterName = ExpressionHelper.GetParameterName(parameter);
  4.             var parameterValue = ExpressionHelper.GetParameterValue(parameter);
  5.  
  6.             if (parameterValue == null)
  7.             {
  8.                 throw new Exception(string.Format("the parameter named ‘{0}’ cannot be null", parameterName));
  9.             }
  10.         }

Obviously if you use this approach you should also check the parameter expression passed is not null and the exception handling should be cleaned up and refactored.

I hope this is useful.

Kind Regards,

Sean McAlinden

No Comments