sheamus
sheamus

Reputation: 3153

get property value in linq expression

I am trying to use a linq expression to validate a phone number in my MVC code. The code looks something like this:

class Person
{
    public HomePhone { get; set; }
}

class Employee
{
    public WorkPhone { get; set; }
}

class Office
{
    Employee Boss { get; set; }
}

class PersonController : Controller
{
    private static ValidatePhoneNumber<M>(Exression<Func<M,string>> propExpr)
    {
        var member = prop.Body as MemberExpression;
        if (member == null)
        {
            throw new ArgumentException("expression must be a member expression, i.e., x => x.MyProperty");
        }

        var propInfo = member.Member as PropertyInfo;
        if (propInfo == null)
        {
            throw new ArgumentException("expression is not a property type.");
        }

        var getter = propExpr.Compile();
        string phoneStr = getter(); //this doesn't work

        if( !/* ... phoneStr is valid phone number */ )
        {
            var propName = propInfo.Name;
            ModelState[propName] = "invalid format for phone number";
        }
    }

    public ActionResult Create(Person p)
    {
        ValidatePhoneNumber( p => p.HomePhone );
                    if( ModelState.IsValid )
                    ....
    }

    public ActionResult CreatOffice(Office o)
    {
        ValidatePhoneNumber( o => o.Boss.WorkPhone );
                    if( ModelState.IsValid )
                    ....
    }
}

I can't quite get a handle on the syntax needed here. What do I need to do to have have a function where I pass in a member property expression, and have access to the name of that property as well as the value of it.

Upvotes: 4

Views: 4038

Answers (2)

SynXsiS
SynXsiS

Reputation: 1898

The p and o you declare in the Create and CreatOffice methods are not the same as the p and o you declare in your lambda expression. In fact you should be getting an error because the identifier already exists in the current scope.

I would modify your method to be an extension method. (It will need to be defined in a static class)

public static ValidatePhoneNumber<M>(this M obj, Expression<Func<M,string>> propExpr)

you can then access the property value from 'obj'. Getting the value would be something like...

propertyInfo.GetValue(obj, null);

Your usage would then be modified to...

public ActionResult Create(Person p)
{
    p.ValidatePhoneNumber( person => person.HomePhone );
                if( ModelState.IsValid )
                ....
}

Upvotes: 2

Phil N.
Phil N.

Reputation: 700

You can get what it seems that you want with Reflection alone.

namespace Forums.LinqToValidatePhoneNumberProperty
{
    using System;
    using System.Linq;
    using System.Reflection;
    using System.Text.RegularExpressions;

    public class PhoneNumberRule
    {
        #region Fields

        static string _usPhonePattern = @"1?\W*([2-9][0-8][0-9])" +
                                        @"\W*([2-9][0-9]{2})\W*" +
                                        @"([0-9]{4})(\se?x?t?(\d*))?";
        static Regex _usPhoneRegex = new Regex( _usPhonePattern );

        #endregion Fields

        #region Methods

        public static void Validate( object target, string propertyName )
        {
            Type targetType = target.GetType();
            PropertyInfo targetProperty = 
                ( from propertyInfo in targetType.GetProperties()
                where ( propertyInfo.Name == propertyName
                && propertyInfo.PropertyType.IsAssignableFrom(  
                    typeof (string ) ) )
                select propertyInfo ).First();

            if ( targetProperty == null )
            {
                throw new InvalidOperationException( "No appropriate property " +
                                                     "could be found on the " + 
                                                     "target object." );
            }

            string testValue = targetProperty.GetValue( target, null ) as string;

            if ( testValue != null && _usPhoneRegex.IsMatch( testValue ) )
            {
                return;
            }
            else
            {
                ModelState[propertyName] = "Not a valid phone number format";
            }
        }

        #endregion Methods
    }
}

A more robust solution might be to use a combination of reflection and custom attributes.

    public class PhoneNumberRule
    {
        #region Fields

        static string _usPhonePattern = @"1?\W*([2-9][0-8][0-9])" +
                                        @"\W*([2-9][0-9]{2})\W*" +
                                        @"([0-9]{4})(\se?x?t?(\d*))?";
        static Regex _usPhoneRegex = new Regex( _usPhonePattern );

        #endregion Fields

        #region Methods

        public static void ValidateProperties( object target )
        {
            Type targetType = target.GetType( );
            var phoneNumberProperties =
                from propertyInfo in targetType.GetProperties( )
                where propertyInfo.GetCustomAttributes(
                    typeof( PhoneNumberAttribute ), true ).Length > 0
                select propertyInfo;
            foreach ( PropertyInfo targetProperty in phoneNumberProperties )
            {
                string value = targetProperty.GetValue( target, null) as string;
                if ( value == null || !_usPhoneRegex.IsMatch( value ) )
                {
                    ModelState[ targetProperty.Name ] = "Not a valid phone number format";
                }
            }
        }

    }

    [AttributeUsage(AttributeTargets.Property)]
    public class PhoneNumberAttribute : Attribute
    {
    }

    public class Person
    {
        [PhoneNumber( )]
        public string HomePhone { get; set; }
    }

Upvotes: 0

Related Questions